Mastodon87
Mastodon87

Reputation: 335

Rename column in Pandas Python not working

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.

Upvotes: 2

Views: 12781

Answers (3)

Simone
Simone

Reputation: 615

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.

Upvotes: 0

fenrisulfr
fenrisulfr

Reputation: 335

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

Upvotes: 0

Dariusz Krynicki
Dariusz Krynicki

Reputation: 2718

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

Upvotes: 1

Related Questions