Pandas Groupby:如何使用两个 lambda 函数? [英] Pandas Groupby: How to use two lambda functions?

查看:83
本文介绍了Pandas Groupby:如何使用两个 lambda 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前可以在 Pandas 中执行以下操作,但 FutureWarning 对我发出了严厉的警告:

I can currently do the following in Pandas, but I get a stern finger wagging from FutureWarning:

grpd = df.groupby("rank").agg({
    "mean": np.mean, "meian": np.median, "min": np.min, "max": np.max, 
    "25th percentile": lambda x: np.percentile(x, 25),
    "75th percentile": lambda x: np.percentile(x, 75)
})

以下抛出错误,因为我有两个 lambda 函数:

The following throws an error because I have two lambda functions:

percentile_25 = lambda x: np.percentile(x, 25)
percentile_75 = lambda x: np.percentile(x, 75)

df = diffs[["User Installs", "rank"]].dropna()
grpd = df.groupby("shopping_rank").agg([
    np.mean, np.median, np.min, np.max, 
    percentile_25, percentile_75
])

这会抛出:

SpecificationError: Function names must be unique, found multiple named <lambda>

我似乎可以完成这项工作的唯一方法(不忽略警告,我可能应该这样做)是使用如下精心设计的过程

The only way I can seem to make this work (without ignoring the warning, which I should probably just do) is with an elaborate process like the following

  1. 使用一个 lambda 函数(第 25 个百分位数)以及我需要的其他所有内容(最小值、最大值等)定义我的 DF
  2. 重命名列以摆脱多索引
  3. 创建另一个 DF,进行另一个分组,这次是我想要的另一列(第 75 个百分位数)
  4. 再次重命名 cols(感谢 MultiIndex!)
  5. 重新加入索引上的原始 DF

我在这里遗漏了什么吗?当然有更好的方法来做我想象中的一件很常见的事情(使用两个不能从 numpy 直接导入的聚合).

Is there something I'm missing here? Surely there's a better way to do what I imagine is a pretty common thing (using two aggregations that aren't directly importable from numpy).

推荐答案

这是一个 已知错误,使用:

def percentile_25(x): return np.percentile(x, 25)
def percentile_75(x): return np.percentile(x, 75)

这篇关于Pandas Groupby:如何使用两个 lambda 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆