4

I'm trying to change my dataframe columns names , i tried two methods but none of them worked . here is my code ;

method 1 :

def func():   
    energy=pd.ExcelFile('Energy Indicators.xls').parse('Energy')
    energy=energy.iloc[16:][['Environmental Indicators: Energy','Unnamed:3','Unnamed: 4','Unnamed: 5']].copy() energy.columns()=['Country',
    'Energy Supply', 'Energy Supply per Capita', '%Renewable']
return energy

method 2:

def func():
    energy=pd.ExcelFile('Energy Indicators.xls').parse('Energy')
    energy=energy.iloc[16:][['Environmental Indicators: Energy','Unnamed: 3','Unnamed: 4','Unnamed: 5']].copy()

    return energy.rename(columns={'Environmental Indicators: Energy': 'Country', 'Unnamed: 3': 'Energy Supply',
                                  'Unnamed: 4': 'Energy Supply per Capita', 'Unnamed: 5': '% Renewable'}, inplace=True)

Both of them return a NoneType Object .

Here is the .xls file i'm working on : https://drive.google.com/file/d/0B80lepon1RrYeDRNQVFWYVVENHM/view?usp=sharing

sali333
  • 125
  • 4
  • 15

2 Answers2

2

in method 1, try to change energy.columns() to energy.columns:

energy.columns =['Country','Energy Supply', 'Energy Supply per Capita', '%Renewable']
1

The inplace=True argument modifies the data frame in place, meaning it won't return anything as a result of the operation.

You can either:
- Return the .rename() without inplace=True, or
- Don't return anything and use the pass-by-reference property to make the update.

andrew_reece
  • 20,390
  • 3
  • 33
  • 58