2 回答
TA贡献1859条经验 获得超6个赞
如果您想要爆炸的“相反”,那么这意味着将其放入解决方案#1 中的列表中。您还可以在解决方案 #2 中作为字符串加入:
用于groupby 函数中的lambda x: x.tolist()列:'APPLICABLE_DAYS'.agg
df = (df.groupby(['NETWORK','config_id'])
.agg({'APPLICABLE_DAYS': lambda x: x.tolist(),'Case':'mean','Delivery':'mean'})
.rename({'Case' : 'Avg_Cases','Delivery' : 'Avg_Delivery'},axis=1)
.reset_index())
df
Out[1]:
NETWORK config_id APPLICABLE_DAYS Avg_Cases Avg_Delivery
0 Grocery 5399 [SUN, MON, TUE, WED] 25 2.5
用于groupby 函数中的lambda x: ",".join(x)列:'APPLICABLE_DAYS'.agg
df = (df.groupby(['NETWORK','config_id'])
.agg({'APPLICABLE_DAYS': lambda x: ",".join(x),'Case':'mean','Delivery':'mean'})
.rename({'Case' : 'Avg_Cases','Delivery' : 'Avg_Delivery'},axis=1)
.reset_index())
df
Out[1]:
NETWORK config_id APPLICABLE_DAYS Avg_Cases Avg_Delivery
0 Grocery 5399 SUN,MON,TUE,WED 25 2.5
如果您正在寻找sum,那么您可以将和列更改mean为。sumCasesDelivery
TA贡献1802条经验 获得超10个赞
你的结果看起来更像是一个总和,而不是平均值;下面的解决方案使用命名聚合:
df.groupby(["NETWORK", "config_id"]).agg(
APPLICABLE_DAYS=("APPLICABLE_DAYS", ",".join),
Total_Cases=("Case", "sum"),
Total_Delivery=("Delivery", "sum"),
)
APPLICABLE_DAYS Total_Cases Total_Delivery
NETWORK config_id
Grocery 5399 SUN,MON,TUE,WED 100 10
如果是平均值,那么您可以将 'sum' 更改为 'mean' :
df.groupby(["NETWORK", "config_id"]).agg(
APPLICABLE_DAYS=("APPLICABLE_DAYS", ",".join),
Avg_Cases=("Case", "mean"),
Avg_Delivery=("Delivery", "mean"),
)
APPLICABLE_DAYS Avg_Cases Avg_Delivery
NETWORK config_id
Grocery 5399 SUN,MON,TUE,WED 25 2.5
添加回答
举报