熊猫栏更名我有一个DataFrame使用熊猫和列标签,我需要编辑,以取代原来的列标签。我想在DataFrame中更改列名A其中原始列名为:['$a', '$b', '$c', '$d', '$e']到['a', 'b', 'c', 'd', 'e'].我将编辑后的列名存储在列表中,但不知道如何替换列名。
3 回答
BIG阳
TA贡献1859条经验 获得超6个赞
.columns
>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})>>> df.columns = ['a', 'b']>>> df
a b0 1 101 2 20
慕桂英4014372
TA贡献1871条经验 获得超13个赞
重命名特定列
df.rename()
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 e0 x x x x x1 x x x x x2 x x x x xdf2 = df.rename({'a': 'X', 'b': 'Y'}, axis=1) # new methoddf2 = df.rename({'a': 'X', 'b': 'Y'}, axis='columns')
df2 = df.rename(columns={'a': 'X', 'b': 'Y'}) # old method df2
X Y c d e0 x x x x x1 x x x x x2 x x x x xinplace=True:
df.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True)df
X Y c d e0 x x x x x1 x x x x x2 x x x x xerrors='raise'rename()
重新分配列标题
df.set_axis()axis=1inplace=False
df2 = df.set_axis(['V', 'W', 'X', 'Y', 'Z'], axis=1, inplace=False)df2 V W X Y Z0 x x x x x1 x x x x x2 x x x x x
inplace=True
df.columns = ['V', 'W', 'X', 'Y', 'Z']df V W X Y Z0 x x x x x1 x x x x x2 x x x x x
添加回答
举报
0/150
提交
取消
