【发布时间】:2019-07-28 08:30:59
【问题描述】:
我有以下数据框,我想将索引从summary 重命名为id:
summary student count
0 error 6
1 yes 1
2 no 1
3 other 9
我尝试过:
newdf = df.reset_index().rename(columns={df.index.name:'foo'}) 给出:
summary index student count
0 0 error 6
1 1 yes 1
2 2 no 1
3 3 other 9
我也试过:df.index.rename('foo', inplace = True) 给出:
summary student count
foo
0 error 6
1 yes 1
2 no 1
3 other 9
我也试过:df.rename_axis('why', inplace = True) 给出:
summary student count
why
0 error 6
1 yes 1
2 no 1
3 other 9
当我做df.dtypes:
summary
student object
count init64
dtype: object
我想要什么:
id student count
0 error 6
1 yes 1
2 no 1
3 other 9
或:
student count
0 error 6
1 yes 1
2 no 1
3 other 9
【问题讨论】: