1

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'})

Screenshot of my code and output in Jupyter

0

4 Answers 4

4

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Alternatively, just reassign it instead of modifying it in place; it is the preferred method, as it is planned to deprecate inplace args in a (probably very distant) future pandas version.
3

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.

Comments

2

You have two options for rename

  1. Update with the result or here directly return it because you don't modify it then
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'})
  1. Change the current object
energy.rename(columns={'Unnamed: 2': 'Country', 'Petajoules': 'Energy Supply', 
                       'Gigajoules': 'Energy Supply per Capita', '%': '% Renewable'},
              inplace=True)

Comments

1

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'})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.