There is a dataframe,df
Index Date Name Category
0 2017-08-09 ABC-SAP 1
1 2017-08-09 CDE-WAS 2
2 2017-08-10 DEF 3
3 2017-08-11 DEF 3
4 2017-08-11 CDE-WAS 2
5 2017-08-11 CDE-WAS 2
I executed this code:
df2=pd.DataFrame(df, columns= ['Name','Category'])
df2= df['Name'].groupby(df['Category']).value_counts()
print(df2)
Then I get:
Index Name
(1,ABC-SAP) 1
(2,CDE-WAS) 3
(3,DEF) 2
The value.counts( ) does not return a descending order on the NAME column. I really want to have it in descending order from highest to lowest count. Any way of doing it?
For me it working nice, but you can test alternative solution:
df2 = df['Name'].groupby(df['Category']).value_counts()
print(df2)
Category Name
Pri CDE-WAS 3
DEF 2
ABC-SAP 1
Name: Name, dtype: int64
df2 = df.groupby('Category')['Name'].value_counts()
print(df2)
Category Name
Pri CDE-WAS 3
DEF 2
ABC-SAP 1
Name: Name, dtype: int64
EDIT:
For sort all values use sort_values
:
df1 = df.groupby('Category')['Name'].value_counts().sort_values(ascending=False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With