简体   繁体   English

熊猫如何在分组的同时在另一列上进行分组

[英]pandas how to sort groupby by group sizes while aggregating on another column

I have the following df , 我有以下df

id    amount
1     20
2     8
1     3
1     2
2     7

I want to groupby the df by id , and sorting the groups by their sizes, 我想groupbydfid ,并通过它们的大小进行排序的群体,

 df.groupby('id').size().sort_values(ascending=False)

but also aggregate on amount of each group to create a separate column total at the same time, 而且聚集的amount每组创建一个单独的塔total在同一时间,

id    amount    total    size
1     20         25       3
1     3          25       3
1     2          25       3
2     8          15       2
2     7          15       2

You can use GroupBy + agg with a list, followed by pd.merge : 您可以在列表中使用GroupBy + agg ,然后使用pd.merge

g = df.groupby('id')['amount'].agg(['size', 'sum'])

res = pd.merge(df, g, left_on='id', right_index=True)\
        .sort_values('size', ascending=False)

print(res)

   id  amount  size  sum
0   1      20     3   25
2   1       3     3   25
3   1       2     3   25
1   2       8     2   15
4   2       7     2   15

暂无
暂无

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

相关问题 如何在对另一列进行排序时对列进行分组? - How to groupby on a column while doing sort on another column? 如何在熊猫中按组仅对另一列的前n%行进行分组? - How to groupby only the top n% rows of another column by group in pandas? 熊猫:如何在汇总列时跳过行? - pandas: how to skip a row while aggregating a column? Pandas DataFrame:按列分组,按日期时间排序,按条件截断分组 - Pandas DataFrame: Groupby Column, Sort By DateTime, and Truncate Group by Condition 如何对熊猫数据框中的一列进行分组,然后对另一列进行sort_values排序? - How to groupby for one column and then sort_values for another column in a pandas dataframe? 在 Pandas Dataframe 中按一列排序,然后按另一列分组? - Sort by one column, then group by another, in Pandas Dataframe? 如何在一列中执行 groupby 并计算 pandas 中每一组中另一列的不同值 - How to perform groupby in one column and count distinct values of another column in each group in pandas 当groupby另一个时,pandas在组中最少获得一列 - pandas get minimum of one column in group when groupby another Pandas/Python groupby,然后计算每组中另一列的平均值 - Pandas/Python groupby and then calculate mean for another column within each group 使用熊猫如何在定义组名称时对相似和不相似的部分列值进行分组 - With pandas how to groupby similar and non-similar partial column values while defining group names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM