使用用户定义的函数聚合 df [英] Aggregate df with user defined function

查看:31
本文介绍了使用用户定义的函数聚合 df的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于使用用户定义的函数聚合 Pandas 数据帧的问题.如果我有一个数据框并在有或没有 groupby 的情况下运行 agg,则在使用内置函数时会聚合结果.另一方面,如果我使用自定义定义的函数,则在使用 groupby 时它会按预期工作.当不使用 groupby 时,不进行聚合.有没有办法在没有 groupby 的情况下使用自定义函数进行聚合?我知道可以只添加一个虚拟变量,但这不是首选的解决方案.测试 1-3 按预期工作,但不能测试 4.

I have a question regarding aggregating pandas dataframes with user defined functions. If i have a dataframe and run agg with or without groupby the result is aggregated when built in functions are used. If i on the other hand use a custom defined function it works as intended when groupby is used. When no groupby is used no aggregation is done. Is there a way to aggregate without a groupby and using a custom function? I know a could just add a dummy variable but that is not the prefered solution. Test1-3 work as intended but not test 4.

df = pd.DataFrame(columns=['a', 'b', 'c'])
n=1000
np.random.seed(0)

df['a'] = np.random.rand(n)
df['a'] = np.random.rand(n)
df['c'] = np.random.randint(1, 4, size=n)

def CoV(_s):
    return pd.Series({'CoV' : np.std(_s)/np.mean(_s)})

test1 = df.agg({'a':['std', np.mean]})
print(test1)

test2 = df.groupby(['c']).agg({'a':['std', np.mean]})
print(test2)

test3 = df.groupby(['c']).agg({'a':[CoV]})
print(test3)

# does not work as intended, no aggregation
test4 = df.agg({'a':[CoV]})
print(test4)

推荐答案

这会给你想要的结果:

df.assign(k=1).groupby('k')['a'].apply(CoV).reset_index(drop=True)

所以你assignk 只是为了将它用于 groupby 然后通过 resetingdroping 索引将其删除.

So you assign k just to use it for groupby and then remove it by reseting and droping index.

这篇关于使用用户定义的函数聚合 df的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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