小编use*_*817的帖子

Pandas DataFrame使用多列聚合函数

有没有办法编写DataFrame.agg方法中使用的聚合函数,可以访问聚合的多个数据列?典型的用例是加权平均加权标准偏差函数.

我希望能够写出类似的东西

def wAvg(c, w):
    return ((c * w).sum() / w.sum())

df = DataFrame(....) # df has columns c and w, i want weighted average
                     # of c using w as weight.
df.aggregate ({"c": wAvg}) # and somehow tell it to use w column as weights ...
Run Code Online (Sandbox Code Playgroud)

python pandas

63
推荐指数
5
解决办法
3万
查看次数

标签 统计

pandas ×1

python ×1