数据框中Pandas中的聚合列(Aggregated Columns in Pandas within a Dataframe)

编程入门 行业动态 更新时间:2024-10-26 10:32:53
数据框中Pandas中的聚合列(Aggregated Columns in Pandas within a Dataframe)

我正在创建具有聚合值的列,其中包含来自Pandas Dataframe的数据,使用groupby()和reset_index()函数,如下所示:

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")

想要将所有这些聚合列(“amount_boxes”和“count_product”)与groupby列“subscription_id”组合在一个数据框中。 有没有办法在函数中执行此操作而不是合并数据帧?

I'm creating columns with aggregated values with the data from Pandas Dataframe using groupby() and reset_index() functions like that:

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". Is there any way to do that ithin a function rather than merging the dataframes?

最满意答案

让我们看一下使用.agg和列和聚合函数的字典。

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

样本输出:

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

Let's look at using .agg with a dictionary of column and aggregation function.

(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

更多推荐

本文发布于:2023-08-06 02:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1442066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框中   数据   Pandas   Dataframe   Columns

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!