我有以下数据集,我想将索引从
我已经尝试了如下语句:
当我执行
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'})
它的作用是重置索引,并将索引列名称更改为 "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