【发布时间】:2014-01-19 01:00:17
【问题描述】:
我正在寻找一种优雅的方式来更改 DataFrame 中的指定列名。
播放数据...
import pandas as pd
d = {
'one': [1, 2, 3, 4, 5],
'two': [9, 8, 7, 6, 5],
'three': ['a', 'b', 'c', 'd', 'e']
}
df = pd.DataFrame(d)
迄今为止我发现的最优雅的解决方案...
names = df.columns.tolist()
names[names.index('two')] = 'new_name'
df.columns = names
我希望有一个简单的单线......这次尝试失败了......
df.columns[df.columns.tolist().index('one')] = 'another_name'
感谢您的任何提示。
【问题讨论】: