我有一个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_cumulativedebit_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)

推荐答案

try :

# credit

mask = df.is_valid.eq(True) & df.account.eq("credit")
df.loc[mask, "credit_cumulative"] = (
    df[mask].groupby(["name", "account"])["transaction"].cumsum()
)

df["credit_cumulative"] = df.groupby("name")["credit_cumulative"].apply(
    lambda x: x.ffill().fillna(0)
)

# debit

mask = df.is_valid.eq(True) & df.account.eq("debit")
df.loc[mask, "debit_cumulative"] = (
    df[mask].groupby(["name", "account"])["transaction"].cumsum()
)

df["debit_cumulative"] = df.groupby("name")["debit_cumulative"].apply(
    lambda x: x.ffill().fillna(0)
)

print(df)

打印:

   name  is_valid account  transaction  credit_cumulative  debit_cumulative
0  Adam      True   debit           10                0.0              10.0
1  Adam     False  credit           10                0.0              10.0
2  Adam      True  credit           10               10.0              10.0
3  Benj      True  credit           10               10.0               0.0
4  Benj     False   debit           10               10.0               0.0
5  Adam      True  credit           10               20.0              10.0

Python相关问答推荐

在Python Attrs包中,如何在field_Transformer函数中添加字段?

如何获取TFIDF Transformer中的值?

我如何使法国在 map 中完全透明的代码?

如何在给定的条件下使numpy数组的计算速度最快?

如何在Raspberry Pi上检测USB并使用Python访问它?

用渐近模计算含符号的矩阵乘法

Pandas Data Wrangling/Dataframe Assignment

在Python中使用if else或使用regex将二进制数据如111转换为001""

Pandas 数据帧中的枚举,不能在枚举列上执行GROUP BY吗?

如何将相同组的值添加到嵌套的Pandas Maprame的倒数第二个索引级别

Python OPCUA,modbus通信代码运行3小时后出现RuntimeError

操作布尔值的Series时出现索引问题

删除Dataframe中的第一个空白行并重新索引列

大型稀疏CSR二进制矩阵乘法结果中的错误

#将多条一维曲线计算成其二维数组(图像)表示

利用广播使减法更有效率

如何将django url参数传递给模板&S url方法?

PYODBC错误(SQL包含-26272个参数标记,但提供了235872个参数,HY 000)

如何从具有完整层次数据的Pandas框架生成图形?

try 理解PyTorch运行错误:try 再次向后遍历图表