2017-05-08 3 views
1

Wenn ichÄndern Art in value_counts

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

I

4 2831 
3 2555 
5 1561 
[...] 

bekommen Wenn ich

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

I

8 225 
9 120 
2 1234 
[...] 

Was ich bekommen m versucht zu tun ist, die Ausgabe in 2, 3, 4 aufsteigender Reihenfolge zu erhalten (die linke numerische Spalte). Kann ich Value_Counts irgendwie ändern oder brauche ich eine andere Funktion?

Antwort

2

Ich glaube, Sie brauchen sort_index, weil linke Spalte index genannt wird:

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 
Verwandte Themen