Rename specific column(s) in pandas

natsuki_2002 picture natsuki_2002 · Nov 3, 2013 · Viewed 259.2k times · Source

I've got a dataframe called data. How would I rename the only one column header? For example gdp to log(gdp)?

data =
    y  gdp  cap
0   1    2    5
1   2    3    9
2   8    7    2
3   3    4    7
4   6    7    7
5   4    8    3
6   8    2    8
7   9    9   10
8   6    6    4
9  10   10    7

Answer

EdChum picture EdChum · Nov 3, 2013
data.rename(columns={'gdp':'log(gdp)'}, inplace=True)

The rename show that it accepts a dict as a param for columns so you just pass a dict with a single entry.

Also see related