我正在寻找一种优雅的方法来更改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'
任何提示都将不胜感激。