仅当条件匹配并按分组时对数据框列求和

时间:2019-11-12 09:07:52

标签: python pandas dataframe

考虑一下我的情况:

数据框:

id    createdId   updatedId   ownerId   value
1     50          50          10        105 
2     51          50          10        240
3     52          50          10        420
4     53          53          10        470
5     40          40          11        320
6     41          40          11        18
7     55          55          12        50
8     57          55          12        412
9     59          55          12        398

仅当ownerId相同且updatedId小于或等于createdId时,我才尝试对“输出”列中的“值”列求和

在我的示例中,输出应为以下数据框:

id    createdId   updatedId   ownerId   value    output
1     50          50          10        105      105
2     51          50          10        240      345  # Add to the previous
3     52          50          10        420      765  # Add to the previous
4     53          53          10        470      1235 # Add to the previous
5     40          40          11        320      320  # Reset because Owner is different
6     41          40          11        18       338
7     55          55          12        50       50
8     57          55          12        412      462
9     59          55          12        398      860

我试图做:

df['output'] = df[['value']].sum(axis=1).where(df['createdId'] > df['updatedId'], 0)

但这不包括所有者检查,而且似乎没有对任何东西进行求和...

我是Panda的新手,请告诉我您如何做?


编辑1:

仅在OwnerId相同时,我试图对[updatedId,createdId]范围内新列“输出”中的所有列“值”求和。

输出:

id    createdId   updatedId   ownerId   value    output
1     50          50          10        105      105
2     51          50          10        240      345  # Add to the previous
3     52          50          10        420      765  # Add to the previous
4     53          53          10        470      470  # Reset because no other value between 53 and 53
5     40          40          11        320      320  # Reset because Owner is different
6     41          40          11        18       338
7     55          55          12        50       50
8     57          55          12        412      462
9     59          55          12        398      860

0 个答案:

没有答案