changing sort in value_counts

前端 未结 1 795
暖寄归人
暖寄归人 2020-12-08 13:13

If I do

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

I get

4    2831
3    2555 
5    1561
[...]


        
相关标签:
1条回答
  • 2020-12-08 13:42

    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
    
    0 讨论(0)
提交回复
热议问题