Categories
dataframe pandas python rename replace

Renaming column names in Pandas

2612

I want to change the column labels of a Pandas DataFrame from

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

to

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

1

2435

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

13

  • 388

    Is it possible to change a single column header name?

    – ericmjl

    Jun 26, 2013 at 17:55


  • 147

    @ericmjl: suppose you want to change the name of the first variable of df. Then you can do something like: new_columns = df.columns.values; new_columns[0] = 'XX'; df.columns = new_columns

    – cd98

    Nov 20, 2013 at 14:18


  • 71

    Looks like you could’ve simply done df.columns.values[0]=’XX’

    – RAY

    Mar 10, 2014 at 7:22

  • 32

    Just kidding, @RAY – don’t do that. Looks like that’s a list generated independent of whatever indexing stores the column name. Does a nice job destroying column naming for your df…

    Mar 11, 2014 at 18:42

  • 554

    @ericmjl yes df.rename(columns = {'$b':'B'}, inplace = True)

    – nachocab

    Sep 11, 2015 at 22:30

2435

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

13

  • 388

    Is it possible to change a single column header name?

    – ericmjl

    Jun 26, 2013 at 17:55


  • 147

    @ericmjl: suppose you want to change the name of the first variable of df. Then you can do something like: new_columns = df.columns.values; new_columns[0] = 'XX'; df.columns = new_columns

    – cd98

    Nov 20, 2013 at 14:18


  • 71

    Looks like you could’ve simply done df.columns.values[0]=’XX’

    – RAY

    Mar 10, 2014 at 7:22

  • 32

    Just kidding, @RAY – don’t do that. Looks like that’s a list generated independent of whatever indexing stores the column name. Does a nice job destroying column naming for your df…

    Mar 11, 2014 at 18:42

  • 554

    @ericmjl yes df.rename(columns = {'$b':'B'}, inplace = True)

    – nachocab

    Sep 11, 2015 at 22:30

486

The rename method can take a function, for example:

In [11]: df.columns
Out[11]: Index([u'$a', u'$b', u'$c', u'$d', u'$e'], dtype=object)

In [12]: df.rename(columns=lambda x: x[1:], inplace=True)

In [13]: df.columns
Out[13]: Index([u'a', u'b', u'c', u'd', u'e'], dtype=object)

4

  • 67

    Nice. This one saved my day: df.rename(columns=lambda x: x.lstrip(), inplace=True)

    – root-11

    Oct 21, 2013 at 22:05

  • 2

    Similar to @root-11 — in my case there was a bullet point character that was not printed in IPython console output, so I needed to remove more than just whitespace (stripe), so : t.columns = t.columns.str.replace(r'[^\x00-\x7F]+','')

    Nov 5, 2015 at 6:30

  • 14

    df.rename(columns=lambda x: x.replace(' ', '_'), inplace=True) is a gem so that we can write df.Column_1_Name instead of writingdf.loc[:, 'Column 1 Name'] .

    Dec 16, 2016 at 15:40


  • How is this not the preferred solution? Only this allows processing a large amount of features names, for instance to allow for dot notation by removing/replacing spaces in the labels, as demonstrated by @LittleBobbyTables

    – error404

    Feb 17 at 13:52