我想使用sort_index对value_counts()进行排序
我的df是这样的
a
1 low
2 high
3 vhigh
...我想对列a进行计数,并按低、地中海、高、高索引对它们进行排序。
df‘..value_count() 20高30低10伏高15
如果我添加sort_index,它将是这样的
高30低10 med 20 vhigh 15
这就是我想要的
低10 med 20高30 vhigh 15
发布于 2018-05-09 06:56:51
考虑到这个问题,这必须是一个定制的排序问题-
value counts
0 med 20
1 high 30
2 low 10
3 vhigh 15这是你做df时得到的value_counts()
将value字段定义为pd.Categorical,并定义顺序-
df['value'] = pd.Categorical(df['value'], ["low", "med", "high", "vhigh"])那就做一种-
df.sort_values('value')输出
value counts
2 low 10
0 med 20
1 high 30
3 vhigh 15https://stackoverflow.com/questions/50247147
复制相似问题