我想从
['$a', '$b', '$c', '$d', '$e']
to
['a', 'b', 'c', 'd', 'e']
我想从
['$a', '$b', '$c', '$d', '$e']
to
['a', 'b', 'c', 'd', 'e']
当前回答
df = pd.DataFrame({'$a': [1], '$b': [1], '$c': [1], '$d': [1], '$e': [1]})
如果新列列表的顺序与现有列的顺序相同,则分配很简单:
new_cols = ['a', 'b', 'c', 'd', 'e']
df.columns = new_cols
>>> df
a b c d e
0 1 1 1 1 1
如果您有一个将旧列名键入到新列名的字典,可以执行以下操作:
d = {'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'}
df.columns = df.columns.map(lambda col: d[col]) # Or `.map(d.get)` as pointed out by @PiRSquared.
>>> df
a b c d e
0 1 1 1 1 1
如果你没有列表或字典映射,你可以通过列表理解去掉前导$符号:
df.columns = [col[1:] if col[0] == '$' else col for col in df]
其他回答
重命名方法可以采用一个函数,例如:
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)
Pandas 0.21+答案
0.21版中的列重命名有一些重要更新。
重命名方法添加了可以设置为columns或1的axis参数。此更新使此方法与panda API的其余部分相匹配。它仍然具有索引和列参数,但不再强制您使用它们。intlace设置为False的set_axis方法允许您使用列表重命名所有索引或列标签。
Pandas 0.21示例+
构造示例DataFrame:
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
使用axis='columns'或axis=1的重命名
df.rename({'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'}, axis='columns')
or
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')
or
df.rename(lambda x: x[1:], axis=1)
将set_axis与列表一起使用,inplace=False
可以为set_axis方法提供一个长度等于列数(或索引)的列表。目前,inplace默认为True,但在未来的版本中,inplace将默认为False。
df.set_axis(['a', 'b', 'c', 'd', 'e'], axis='columns', inplace=False)
or
df.set_axis(['a', 'b', 'c', 'd', 'e'], axis=1, inplace=False)
为什么不使用df.columns=[‘a’,‘b’,‘c’,‘d’,‘e’]?
像这样直接分配列没有错。这是一个非常好的解决方案。
使用set_axis的优点是它可以作为方法链的一部分使用,并返回DataFrame的新副本。如果没有它,在重新分配列之前,必须将链的中间步骤存储到另一个变量。
# 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()
让我们通过一个小例子来理解重命名。。。
使用映射重命名列:df=pd.DataFrame({“A”:[1,2,3],“B”:[4,5,6]})#创建列名为A和B的dfdf.reame({“A”:“new_A”,“B”:“new_B”},axis='columns',inplace=True)#用'new_A'重命名列A,用'new_B'重命名列B输出:新a新b0 1 41 2 52 3 6使用映射重命名索引/Row_Name:df.reame({0:“x”,1:“y”,2:“z”},axis='index',inplace=True)#行名称被'x'、'y'和'z'替换。输出:新a新bx 142015年z 3 6
我的方法是通用的,您可以通过逗号分隔分隔符=variable来添加额外的分隔符,并对其进行未来验证。
工作代码:
import pandas as pd
import re
df = pd.DataFrame({'$a':[1,2], '$b': [3,4],'$c':[5,6], '$d': [7,8], '$e': [9,10]})
delimiters = '$'
matchPattern = '|'.join(map(re.escape, delimiters))
df.columns = [re.split(matchPattern, i)[1] for i in df.columns ]
输出:
>>> df
$a $b $c $d $e
0 1 3 5 7 9
1 2 4 6 8 10
>>> df
a b c d e
0 1 3 5 7 9
1 2 4 6 8 10
df = pd.DataFrame({'$a': [1], '$b': [1], '$c': [1], '$d': [1], '$e': [1]})
如果新列列表的顺序与现有列的顺序相同,则分配很简单:
new_cols = ['a', 'b', 'c', 'd', 'e']
df.columns = new_cols
>>> df
a b c d e
0 1 1 1 1 1
如果您有一个将旧列名键入到新列名的字典,可以执行以下操作:
d = {'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'}
df.columns = df.columns.map(lambda col: d[col]) # Or `.map(d.get)` as pointed out by @PiRSquared.
>>> df
a b c d e
0 1 1 1 1 1
如果你没有列表或字典映射,你可以通过列表理解去掉前导$符号:
df.columns = [col[1:] if col[0] == '$' else col for col in df]