我终于决定摆脱
FutureWarning: using a dict on a Series for aggregation
is deprecated and will be removed in a future version
在对熊猫系列进行聚合时收到的弃用警告。
这里有一些虚拟数据:
df = pd.DataFrame(
[["foo", 10, 1],
["foo", 20, 1],
["foo", 30, 1],
["bar", 1, 1],
["bar", 2, 1]],
columns=["cat", "val", "val2"]
)
我的目标是在分组后的Wow Mean
列上获得两个聚合---平均值为Wow Max
和最大值为val
-由cat
。
在以前的版本中,我曾经这样做:
df.groupby("cat")["val"].agg({"Wow Mean": "mean", "Wow Max": "max"})
但这会发出上述警告。
以下所有内容返回正确的结果 警告:
# Rather similar but considered unstable
# https://stackoverflow.com/questions/44635626/rename-result-columns-from-pandas-aggregation-futurewarning-using-a-dict-with?answertab=oldest#comment88678191_50697003
df.groupby("cat").agg({"val": [("Wow Mean", "mean"), ("Wow Max", "max")]})
# Much more cumbersome
df.groupby("cat")["val"].agg(["mean", "max"]).rename({"mean": "Wow Mean", "max": "Wow Max"}, axis=1)
# Yet more cumbersome (and working only with version 0.25+) [*]:
df.groupby('cat')["val"].agg(**{'Wow mean':pd.NamedAgg('Is it mean?','mean'), 'Wow max':pd.NamedAgg('Maxmin','max')})
建议使用here的替代方案[*]
。
最后,我认为:
df.groupby("cat")['val'].agg(Mean='mean', Max='max')
应该可以,但不能。此外,在这种方法中,结果列的名称只能是有效的python名称,因此对我的目标没有太大帮助。
那么实现我提到的目标的最佳/正确/ Pythonic / Pandaic方法是什么?