如何使用pandas groupby函数基于groupby值应用公式 [英] How do I use pandas groupby function to apply a formula based on the groupby value

查看:86
本文介绍了如何使用pandas groupby函数基于groupby值应用公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题可能有点令人困惑,所以让我解释一下.我有一个信息数据框,希望根据唯一的订单ID进行分组,该ID将产生以下列:

My question may be a little confusing, so let me explain. I have a dataframe of information that I would like to group by the unique order id that will produce the following columns:

sum qty =每个订单ID执行的总金额. csv =这是每个订单ID的csv列的总和除以订单ID的已执行金额的总和.

sum qty = the total amount that was executed per order id. csv = this is the sum of the csv column per order id divided by the sum of the executed amount of the order id.

第一列很容易使用groupby创建,这是我遇到问题的第二列.这是我正在使用的示例数据:

The first column is easy to create with groupby, it's the second column that I am having issues with. Here is sample data that I am working with:

    qty     sym     price   ordrefno    ord_bidprice    ord_askprice    csv
0   -25000  TEST    0.044   984842      0.0435          0.044          12.5
1   100     TEST    0.0443  984702      0.0435          0.044          0.03
2   -10000  TEST    0.0405  983375      0.039           0.0405         15
3   -100    TEST    0.0443  984842      0.0435          0.044          0.03

这是我的代码:

cs1 = lambda x: np.sum(test.csv / test.qty)
f2 = {'qty' : ['sum'], 'csv' : {'es' : cs1}}

agg_td = trades.groupby('ordrefno').agg(f2)

推荐答案

编写命名的函数并使用apply可以起作用:

Writing a named funtion and using apply works:

def func(group):
    sum_ = group.qty.sum()
    es = (group.csv / group.qty).sum()
    return pd.Series([sum_, es], index=['qty', 'es'])

trades.groupby('ordrefno').apply(func)

结果:

            qty     es
ordrefno               
983375   -10000 -0.0015
984702      100  0.0003
984842   -25100 -0.0008

这篇关于如何使用pandas groupby函数基于groupby值应用公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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