pandas 对每组中两列之间的差异求和 [英] pandas sum the differences between two columns in each group

查看:69
本文介绍了pandas 对每组中两列之间的差异求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 df 看起来像,

I have a df looks like,

A               B              C    D
2017-10-01      2017-10-11     M    2017-10
2017-10-02      2017-10-03     M    2017-10
2017-11-01      2017-11-04     B    2017-11
2017-11-08      2017-11-09     B    2017-11
2018-01-01      2018-01-03     A    2018-01

ABdtype分别是datetime64CDstrings;

the dtype of A and B are datetime64, C and D are of strings;

我喜欢 groupby CD 并得到 BA<的区别/code>,

I like to groupby C and D and get the differences between B and A,

df.groupby(['C', 'D']).apply(lambda row: row['B'] - row['A'])

但我不知道如何总结每个组中的这些差异并将值分配给一个新列,比如 E,可能在一个新的 df 中,>

but I don't know how to sum such differences in each group and assign the values to a new column say E, possibly in a new df,

C    D          E
M    2017-10    11
M    2017-10    11
B    2017-11    4
B    2017-11    4
A    2018-01    2

推荐答案

基于你的代码

df.merge(df.groupby(['C', 'D']).apply(lambda row: row['B'] - row['A']).sum(level=[0,1]).reset_index())
Out[292]: 
           A          B  C        D       0
0 2017-10-01 2017-10-11  M  2017-10 11 days
1 2017-10-02 2017-10-03  M  2017-10 11 days
2 2017-11-01 2017-11-04  B  2017-11  4 days
3 2017-11-08 2017-11-09  B  2017-11  4 days
4 2018-01-01 2018-01-03  A  2018-01  2 days

这篇关于pandas 对每组中两列之间的差异求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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