Pandas 从值更改索引类型\u counts()
我正在尝试将Pandas 从值更改索引类型\u counts(),pandas,dataframe,series,Pandas,Dataframe,Series,我正在尝试将value\u counts()之后的索引类型从int更改为string 输出: 40.0 1448 45.0 28558 50.0 83675 55.0 96377 60.0 47351 65.0 13226 70.0 2602 75.0 568 80.0 72 100.0 52 105.0 53 Name: value, dtype: int64 预期产出: 40.0
value\u counts()之后的索引类型从int
更改为string
输出:
40.0 1448
45.0 28558
50.0 83675
55.0 96377
60.0 47351
65.0 13226
70.0 2602
75.0 568
80.0 72
100.0 52
105.0 53
Name: value, dtype: int64
预期产出:
40.0 1448
45.0 28558
50.0 83675
55.0 96377
60.0 47351
65.0 13226
70.0 2602
75.0 568
80.0 72
100.0 52
105.0 53
Name: value, dtype: string
如果需要转换排序后的索引值,如40.0
,请使用rename
:
df['value'].value_counts().sort_index().rename(index=str)
如果需要转换计数值,如1448
使用:
为什么不添加.astype(str)
?
df['value'].value_counts().sort_index().rename(index=str)
df['value'].value_counts().sort_index().astype(str)