Reputation: 11
Why isn't my attempt to rename columns working? I could swear I am following the right syntax and I am not getting any errors... it's simply not doing it.
import numpy as np
import pandas as pd
header = list(range(17))
energy = pd.read_excel('Energy Indicators.xls',
usecols = [2, 3, 4, 5],
skiprows = list(range(17)),
skipfooter = 38)
energy.rename(columns={'Unnamed: 2': 'Country',
'Petajoules': 'Energy Supply',
'Gigajoules': 'Energy Supply per Capita',
'%': '% Renewable'})
Upvotes: 1
Views: 704
Reputation: 54148
You have two options for rename
energy = energy.rename(columns={'Unnamed: 2': 'Country', 'Petajoules': 'Energy Supply',
'Gigajoules': 'Energy Supply per Capita', '%' : '% Renewable'})
return energy.rename(columns={'Unnamed: 2': 'Country', 'Petajoules': 'Energy Supply',
'Gigajoules': 'Energy Supply per Capita', '%' : '% Renewable'})
energy.rename(columns={'Unnamed: 2': 'Country', 'Petajoules': 'Energy Supply',
'Gigajoules': 'Energy Supply per Capita', '%': '% Renewable'},
inplace=True)
Upvotes: 2
Reputation: 497
You have two options. You can re-assign the output of energy.rename()
by using energy=energy.rename()
or you can add the optional argument inplace=True
.
From the documentation for DataFrame.rename
:
inplace : bool, default False
Whether to return a new DataFrame. If True then value of copy is ignored.
Upvotes: 3
Reputation: 15
You dont necessarily need inplace=True. you just have to save it back to your original df
energy = energy.rename(columns={'Unnamed: 2': 'Country',
'Petajoules': 'Energy Supply',
'Gigajoules': 'Energy Supply per Capita',
'%': '% Renewable'})
Upvotes: 1
Reputation: 5140
What you are missing is inplace=True
to rename and apply on the original dataframe object.
Note: inplace=True returns None inplace=False returns a copy of the object with the operation performed.
import pandas as pd
df_sorted = pd.DataFrame({'duration':[1,0,2,0],'Count':[23,23,2,1]})
df_sorted.rename(columns={'duration':'NewName'},inplace=True)
print(df_sorted)
Upvotes: 4