1

有一个df带有列的数据框id,就像这样priceunits


id | price | units
---+-------+--------
1  | 10    | 1
1  | 15    | 4
1  | 13    | 3
2  | 5     | 12
2  | 1     | 20

我可以将多个功能应用于特定列,例如


df.groupby("id").agg({"price":["mean","max"], "units":["mean","max"])

由于我们有max两次mean,重命名它们可能会很好(我知道输出是一个多列索引)以便能够区分它们。

通常在使用时.agg我们可以指定名称,df.groupby("id")["price"](max_price="max")但在解析 dict 时似乎不一样,例如

df.groupby("id").agg({"price":[(mean_price="mean"),(max_price="max")],
 "units":[(mean_unit="mean"),(max_unit="max")]})
4

1 回答 1

2

使用命名聚合 - 格式不同 - new column namewith 元组用于columns namewith aggregation function

df1 = (df.groupby("id").agg(mean_price= ("price","mean"),
                            max_price=("price","max"),
                            mean_unit=("units","mean"),
                            max_unit=("units","max")))
print (df1)
    mean_price  max_price  mean_unit  max_unit
id                                            
1    12.666667         15   2.666667         4
2     3.000000          5  16.000000        20

如果想要传递字典,另一种解决方案是使用 dict with**来解包参数:

df1 = (df.groupby("id").agg(**{'mean_price':("price","mean"),
                               'max_price':("price","max"),
                               'mean_unit':("units","mean"),
                               'max_unit':("units","max")}))
于 2020-11-27T11:09:53.037 回答