Python 如何在pandas中使用具有特定顺序的sort_索引

Python 如何在pandas中使用具有特定顺序的sort_索引,python,pandas,Python,Pandas,我想使用sort_index对值_计数()进行排序 我的df是这样的 a 1 low 2 high 3 vhigh ... 我想对a列进行计数,并按low、med、high和vhigh的索引对它们进行排序 如果我添加sort_索引,它将是这样的 a 1 low 2 high 3 vhigh ... 这就是我想要的 鉴于这个问题,这必须是一个自定义排序问题- value counts 0 med 20 1 high 30 2 low

我想使用sort_index对值_计数()进行排序

我的df是这样的

   a
1 low
2 high
3 vhigh
...
我想对a列进行计数,并按low、med、high和vhigh的索引对它们进行排序

如果我添加sort_索引,它将是这样的

   a
1 low
2 high
3 vhigh
...
这就是我想要的


鉴于这个问题,这必须是一个自定义排序问题-

   value  counts
0    med      20
1   high      30
2    low      10
3  vhigh      15
这是执行
值\u counts()

字段定义为
pd.category
并定义顺序-

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      15

值\默认情况下,它将按值降序排序。我认为你们的样品输出不正确。提供实际的数据框架我在示例中没有使用sort_索引,如果我添加它的话。结果将按降序排序,但它仍然不是答案我期望的是什么为什么
vhigh 15
位于底部?谢谢,但实际上低、中、高、vhigh是1的值columns@Haliris是,对
值\u counts()
输出数据帧的输出执行此操作
df.sort_values('value')
   value  counts
2    low      10
0    med      20
1   high      30
3  vhigh      15