2017-05-08 40 views
1

如果我做在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 
[...] 

我一個m試圖做的是以2,3,4升序(左數字列)獲得輸出。我可以改變value_counts或我需要使用不同的功能。

回答

2

我想你需要sort_index,因爲左邊的列被稱爲index

mt = mobile.PattLen.value_counts().sort_index() 

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