数据框中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 7Let'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更多推荐
发布评论