我有一个Pandas 数据框,格式如下
name | is_valid | account | transaction
Adam | True | debit | +10
Adam | False | credit | +10
Adam | True | credit | +10
Benj | True | credit | +10
Benj | False | debit | +10
Adam | True | credit | +10
我想创建两个新列credit_cumulative
和debit_cumulative
.
在上述示例中,结果应为:
from | is_valid | account | transaction | credit_cumulative | debit_cumulative
Adam | True | debit | +10 | 0 | 10
Adam | False | credit | +10 | 0 | 10
Adam | True | credit | +10 | 10 | 10
Benj | True | credit | +10 | 10 | 0
Benj | False | debit | +10 | 10 | 0
Adam | True | credit | +10 | 20 | 10
举例来说,第一行是Adam,account是debit,is\u valid是true,所以我们将debit\u cumulative增加10.
对于第二行,is\u valid为负.所以交易不算在内.姓名为Adam,credit\u cumulative和debit\u cumulative将保持不变.
所有行的行为都应如此.
以下是我描述的原始数据的代码:
d = {'name': ['Adam', 'Adam', 'Adam', 'Benj', 'Benj', 'Adam'], 'is_valid': [True, False, True, True, False, True], 'account': ['debit', 'credit', 'credit', 'credit', 'debit', 'credit'], 'transaction': [10, 10, 10, 10, 10, 10]}
df = pd.DataFrame(data=d)