Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to rename columns in Pandas ?

I have a DataFrame using Pandas and column labels that I need to edit to replace the original column labels.

I want to change the column names in a DataFrame A where the original column names are:

['$a', '$b', '$c', '$d', '$e']

to

['a', 'b', 'c', 'd', 'e'].

I have the edited column names stored it in a list, but I don't know how to replace the column names.
by

2 Answers

RoliMishra
Just assign it to the .columns attribute:

>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df
$a $b
0 1 10
1 2 20

>>> df.columns = ['a', 'b']
>>> df
a b
0 1 10
1 2 20
sandhya6gczb
For renaming the columns use

df.rename(columns={'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'}, inplace=True)

Login / Signup to Answer the Question.