pandas 熊猫 value_counts() 不是降序排列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47899830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas value_counts( ) not in descending order
提问by Bode
There is a dataframe,df
有一个数据框,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?
value.counts() 不返回 NAME 列的降序。我真的很想按从最高到最低计数的降序排列。有什么办法吗?
采纳答案by jezrael
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
:
对于排序所有值使用sort_values
:
df1 = df.groupby('Category')['Name'].value_counts().sort_values(as??cending=False)