简体   繁体   English

使用 Pandas 数据框聚合后无法对值进行排序

[英]Can't sort values after aggregation using Pandas dataframe

I have the following dataframe:我有以下数据框:

df[['ID','Team']].groupby(['Team']).agg([('total','count')]).reset_index("total").sort_values("count")

I basically, need to count the number of IDs by Team and then sort by the total number of IDs.我基本上需要按团队计算 ID 的数量,然后按 ID 的总数进行排序。

The aggregation part it's good and it gives me the expected result.聚合部分很好,它给了我预期的结果。 But when I try the sort part I got this:但是当我尝试排序部分时,我得到了这个:

KeyError: 'Requested level (total) does not match index name (Team)'

What I am doing wrong?我做错了什么?

Use names aggregation for specify new columns names in aggregate function, remove total from DataFrame.reset_index :使用名称聚合在聚合函数中指定新列的名称,从DataFrame.reset_index删除total

df = pd.DataFrame({
        'ID':list('abcdef'),
        'Team':list('aaabcb')
})

df = df.groupby('Team').agg(count=('ID','count')).reset_index().sort_values("count") 
print (df)
  Team  count
2    c      1
1    b      2
0    a      3

Your solution should be changed by specify column after groupby for processing, then specify new column name with aggregate function in tuple and last also remove total from reset_index :您的解决方案应该通过在groupby之后指定列进行处理来更改,然后在元组中使用聚合函数指定新列名,最后还从reset_index删除total

df = df.groupby('Team')['ID'].agg([('count','count')]).reset_index().sort_values("count")
print (df)
  Team  count
2    c      1
1    b      2
0    a      3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM