91
df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}) # Or rename the existing DataFrame (rather than creating a copy) df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)
df = pd.DataFrame('x', index=range(3), columns=list('abcde')) df a b c d e 0 x x x x x 1 x x x x x 2 x x x x x
df2 = df.rename({'a': 'X', 'b': 'Y'}, axis=1) # new method df2 = df.rename({'a': 'X', 'b': 'Y'}, axis='columns') df2 = df.rename(columns={'a': 'X', 'b': 'Y'}) # old method df2 X Y c d e 0 x x x x x 1 x x x x x 2 x x x x x
df.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True) df X Y c d e 0 x x x x x 1 x x x x x 2 x x x x x
df2 = df.set_axis(['V', 'W', 'X', 'Y', 'Z'], axis=1, inplace=False) df2 V W X Y Z 0 x x x x x 1 x x x x x 2 x x x x x
df.columns = ['V', 'W', 'X', 'Y', 'Z'] df V W X Y Z 0 x x x x x 1 x x x x x 2 x x x x x
82
>>> 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
77
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)
60
df.columns = df.columns.str.replace('$', '')
58
df = pd.DataFrame({'$a':[1,2], '$b': [3,4], '$c':[5,6], '$d':[7,8], '$e':[9,10]}) $a $b $c $d $e 0 1 3 5 7 9 1 2 4 6 8 10
df.rename({'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'}, axis='columns')
df.rename({'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'}, axis=1)
a b c d e 0 1 3 5 7 9 1 2 4 6 8 10
df.rename(columns={'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'})
df.rename(lambda x: x[1:], axis='columns')
df.rename(lambda x: x[1:], axis=1)
df.set_axis(['a', 'b', 'c', 'd', 'e'], axis='columns', inplace=False)
df.set_axis(['a', 'b', 'c', 'd', 'e'], axis=1, inplace=False)
# new for pandas 0.21+ df.some_method1() .some_method2() .set_axis() .some_method3() # old way df1 = df.some_method1() .some_method2() df1.columns = columns df1.some_method3()