Wesley Odom
Wesley Odom

Reputation: 11

Python: Renaming columns with pandas

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

Upvotes: 1

Views: 704

Answers (4)

azro
azro

Reputation: 54148

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)

Upvotes: 2

AlbinoRhino
AlbinoRhino

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

r9t
r9t

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

S.N
S.N

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

Related Questions