ziggy
ziggy

Reputation: 1538

pandas rename columns then assign to a new dataframe

python 2.7 - I want to create a new dataframe with renaming columns from a different dataframe

for example x is a pandas DF and I want to rename the columns and save it into y

this example returns NONE

x=pd.read_excel(r'C:\hm.xlsx')
y=x.rename(columns={'Zip': 'Zipcode'},inplace='False')
print y

however if I do

x=pd.read_excel(r'C:\hm.xlsx')
x.rename(columns={'Zip': 'Zipcode'},inplace='True')
print x

this correctly renames it -- what do I need to do

Upvotes: 0

Views: 461

Answers (1)

Anonymous Dodo
Anonymous Dodo

Reputation: 271

Try This

x = pd.read_excel(r'C:\hm.xlsx')
y = x.copy()
y.rename(columns={'Zip': 'Zipcode'},inplace='True')
print(y)

x.rename() is a function that does not return anything but rather alters x. By setting y = x.rename() you are therefore setting y to NoneType. y = x.copy() is necessary to create a deep copy rather than a pointer to x, so you can alter y without altering x

Upvotes: 3

Related Questions