更改value_counts中的排序 [英] changing sort in value_counts

查看:1391
本文介绍了更改value_counts中的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我这样做

mt = mobile.PattLen.value_counts()   # sort True by default

我知道

4    2831
3    2555 
5    1561
[...]

如果我愿意

mt = mobile.PattLen.value_counts(sort=False) 

我知道

8    225
9    120
2   1234 
[...]

我要执行的操作是按2、3、4升序(左侧数字列)获取输出.我可以以某种方式更改value_counts还是需要使用其他函数.

What I am trying to do is get the output in 2, 3, 4 ascending order (the left numeric column). Can I change value_counts somehow or do I need to use a different function.

推荐答案

我认为您需要

I think you need sort_index, because the left column is called index. The full command would be mt = mobile.PattLen.value_counts().sort_index(). For example:

mobile = pd.DataFrame({'PattLen':[1,1,2,6,6,7,7,7,7,8]})
print (mobile)
   PattLen
0        1
1        1
2        2
3        6
4        6
5        7
6        7
7        7
8        7
9        8

print (mobile.PattLen.value_counts())
7    4
6    2
1    2
8    1
2    1
Name: PattLen, dtype: int64


mt = mobile.PattLen.value_counts().sort_index()
print (mt)
1    2
2    1
6    2
7    4
8    1
Name: PattLen, dtype: int64

这篇关于更改value_counts中的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆