2

I'm trying to rename one column with the rename function. However, nothing changes. Could someone help me?

The code i have tried is the following:

Snd_Mer_Vol_Output = Snd_Mer_Vol_Output.rename(columns={'(1,Snd_Mer_Vol_Probability')': 'Snd_Mer_Vol_Probability'})


File "<ipython-input-28-057b6859dfa6>", line 1
Snd_Mer_Vol_Output = Snd_Mer_Vol_Output.rename(columns={'(1, 'Snd_Mer_Vol_Probability')': 'Snd_Mer_Vol_Probability'})
                                                                                        ^
SyntaxError: invalid syntax

Thank you.

fenrisulfr
  • 325
  • 1
  • 7
Mastodon87
  • 325
  • 3
  • 6
  • 14
  • 3
    Your quotations marks are messing up. You change the outer quotation to double quotes. – TYZ Mar 22 '18 at 13:15
  • I changed, the error is not appearing, but the name is still the same – Mastodon87 Mar 22 '18 at 13:20
  • Snd_Mer_Vol_Output = Snd_Mer_Vol_Output.rename(columns={"(1, 'Snd_Mer_Vol_Probability')": "Snd_Mer_Vol_Probability"}) – Mastodon87 Mar 22 '18 at 13:20
  • Snd_Mer_Vol_Output.columns Out[36]: Index(['CUSTID', 'SND_MER_VOL_Predictions', (1, 'Snd_Mer_Vol_Probability')], dtype='object') – Mastodon87 Mar 22 '18 at 13:20
  • The title says you are renaming a **function**, the explanation says you are renaming a **variable** and the code actually renames a **column** in a dataframe. Please, edit your question to it's clear what are you asking. –  Mar 22 '18 at 13:22
  • you're right, sorry for that – Mastodon87 Mar 22 '18 at 13:28

3 Answers3

1

following the answer from Yilun Zhang:

import pandas as pd
df = pd.DataFrame({"(1, 'Snd_Mer_Vol_Probability')": [1, 2, 3], "B": [4, 5, 6]})
print (df)

df = df.rename(columns={"(1, 'Snd_Mer_Vol_Probability')": 'Snd_Mer_Vol_Probability'})
print (df)

   (1, 'Snd_Mer_Vol_Probability')  B
0                               1  4
1                               2  5
2                               3  6
   Snd_Mer_Vol_Probability  B
0                        1  4
1                        2  5
2                        3  6
Dariusz Krynicki
  • 2,544
  • 1
  • 22
  • 47
0

Could you try this instead? Assuming I've understood what you're trying to do, which is rename a column called (1, 'Snd_Mer_Vol_Probability') to Snd_Mer_Vol_Probability

Snd_Mer_Vol_Output.rename(columns={"(1, 'Snd_Mer_Vol_Probability')": 'Snd_Mer_Vol_Probability'},inplace=True)

EDIT:

You actually need:

Snd_Mer_Vol_Output.rename(columns={(1, 'Snd_Mer_Vol_Probability'): 'Snd_Mer_Vol_Probability'},inplace=True)

As your .columns output below shows that the column name is a tuple and not a string, so it doesn't need quotes (double or otherwise) around it, as you can see I've done an example myself:

df = pd.DataFrame({(1,'hello'):[1],'test':[2]})
print(df)

>>   test  (1, hello)
>>      2           1

df.rename(columns={(1,'hello'):'testing2'},inplace=True)
print(df)

>>   test   testing2
>>      2          1
fenrisulfr
  • 325
  • 1
  • 7
  • I tried it, and now I'm having this error: Snd_Mer_Vol_Output.columns Traceback (most recent call last): File "", line 1, in Snd_Mer_Vol_Output.columns AttributeError: 'NoneType' object has no attribute 'columns' – Mastodon87 Mar 22 '18 at 13:32
  • Have you defined the DataFrame before running this? If you run “print(Snd_Mer_Vol_Output)” what happens? – fenrisulfr Mar 22 '18 at 13:37
  • yep, the data-frame exists – Mastodon87 Mar 22 '18 at 13:39
  • these are all the variables: Snd_Mer_Vol_Output.columns Out[47]: Index(['CUSTID', 'SND_MER_VOL_Predictions', (1, 'Snd_Mer_Vol_Probability')], dtype='object') – Mastodon87 Mar 22 '18 at 13:39
  • I'm trying the rename the 3rd variable – Mastodon87 Mar 22 '18 at 13:40
  • Ok, try this: `Snd_Mer_Vol_Output.rename(columns={(1, 'Snd_Mer_Vol_Probability'): 'Snd_Mer_Vol_Probability'},inplace=True)` I've removed the quotes around the column name because it appears to be a tuple, not a string – fenrisulfr Mar 22 '18 at 14:52
0

Ran into the same problem with one specific DF after I concatenated a Pandas DF with two Pandas Series. Tried to use several variants of df.rename() listed below. None of them worked.

# using column name and axis 
df = df.rename({'oldName1':'newName1', 'oldName2':'newName2'}, axis = 'columns')

# using column index and axis
df = df.rename({28:'newName1', 29:'newName2'}, axis = 'columns')

# using column name
df = df.rename(columns = {'oldName1':'newName1', 'oldName2':'newName2'})

# using column name and inplace function
df.rename(columns = {'oldName1':'newName1', 'oldName2':'newName2'}, inplace = True)

# using column index and inplace function
df.rename(columns = {28:'newName1', 29:'newName2'}, inplace = True)

Also tried above suggestion df.rename(columns={(28, 'newName1'): 'newName1'}, inplace = True, which did not work.

What worked is this: df.columns.values[27]= 'newName1'
This is of course not ideal as it needs to be done individually for each column. As I only had 2 columns to rename this is ok for me. If possible I recommend to use df.rename(), but if it just doesn't work this may be an alternative.

Simone
  • 497
  • 5
  • 19