简体   繁体   English

数据框内熊猫的汇总列

[英]Aggregated Columns in Pandas within a Dataframe

I'm creating columns with aggregated values with the data from Pandas Dataframe using groupby() and reset_index() functions like that: 我正在使用像这样的groupby()和reset_index()函数使用来自Pandas Dataframe的数据创建具有聚合值的列:

df=data.groupby(["subscription_id"])["count_boxes"].sum().reset_index(name="amount_boxes")

df1=data.groupby(["subscription_id"])["product"].count().reset_index(name="count_product")

Want to combine all these aggregated columns ("amount_boxes" and "count_product") in one dataframe with groupby column "subscription_id". 要将一个数据框中的所有这些汇总列(“ amount_boxes”和“ count_product”)与groupby列“ subscription_id”组合在一起。 Is there any way to do that ithin a function rather than merging the dataframes? 有什么方法可以在函数中完成此操作,而不是合并数据框?

Let's look at using .agg with a dictionary of column and aggregation function. 让我们看看将.agg与列和聚合函数的字典一起使用。

(df.groupby('Subscription_id')
   .agg({'count_boxes':'sum','product':'count'})
   .reset_index()
   .rename(columns={'count_boxes':'amount_boxes','product':'count_product'}))

Sample Output: 样本输出:

   Subscription_id  amount_boxes  count_product
0                1            16              2
1                2            39              6
2                3            47              7

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

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