小编sou*_*rav的帖子

在 Pandas 中聚合多列时如何重置索引

我有我试图分组的数据框,它看起来像这样

Cust_ID Store_ID month lst_buy_dt1  purchase_amt    
 1       20       10     2015-10-07  100
 1       20       10     2015-10-09  200
 1       20       10     2015-10-20  100
Run Code Online (Sandbox Code Playgroud)

我需要的最大的ls_buy_dt和最大或购买金额为每个cust_IDStore_ID在不同的数据帧每个月组合。示例输出:

Cust_ID Stored_ID month max_lst_buy_dt tot_purchase_amt
 1       20        10      2015-10-20     400
Run Code Online (Sandbox Code Playgroud)

我的代码在下面。

aggregations = {
    'lst_buy_dt1': { # Get the max purchase date across all purchases in a month
    'max_lst_buy_dt': 'max',       
    },
    'purchase_amt': {     # Sum the purchases 
    'tot_purchase': 'sum',   # Find the max, call the result "max_date"
    }
}

grouped_at_Cust=metro_sales.groupby(['cust_id','store_id','month']).agg(aggregations).reset_index()
Run Code Online (Sandbox Code Playgroud)

我能够获得正确的聚合。但是,数据框在列中包含一个我无法删除的附加索引。无法显示,但这是结果

list(grouped_at_Cust.columns.values) …
Run Code Online (Sandbox Code Playgroud)

group-by aggregate-functions pandas

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

aggregate-functions ×1

group-by ×1

pandas ×1