如何基于熊猫数据框中的其他列值更新“余额”列

时间:2020-06-04 08:19:35

标签: python python-3.x pandas dataframe

我有以下数据框。

         Date    Status   Amount  Balance
0  06-10-2000   Deposit    40.00     40.0
1  09-12-2002  Withdraw  1000.00      NaN
2  27-06-2001   Deposit    47.00      NaN
3  07-12-2021  Withdraw   100.00      NaN
4  06-10-2022   Deposit   120.00      NaN
5  06-10-2000   Deposit    40.00      NaN
6  09-12-2024  Withdraw    50.00      NaN

目标是根据存款还是取款来更新余额,初始余额=起始金额。因此将其硬编码为40.0。

下面是我的代码,以某种方式我没有得到预期的结果。

预期结果:

         Date    Status   Amount  Balance
0  06-10-2000   Deposit    40.00     40.0
1  09-12-2002  Withdraw  1000.00    -960.0
2  27-06-2001   Deposit    47.00    -913.0
3  07-12-2021  Withdraw   100.00    -1013.0
4  06-10-2022   Deposit   120.00    -893.0
5  06-10-2000   Deposit    40.00    -853.0
6  09-12-2024  Withdraw    50.00    -903.0

我在代码中做错什么,代码在下面

import pandas as pd
with open(r"transactions.txt", "r") as Account:
    details = Account.read().split(",")
print("details of txt",details)

df=pd.DataFrame(details)

fg=df[0].str.extract('(?P<Date>.*) (?P<Status>.*) (?P<Amount>.*)')
print(fg)

fg['Amount'] = fg.Amount.str.replace('$','') #removing $ sign
#setting first row value of balance as 40, as equal to amount in 1st row
fg.loc[fg.index[0], 'Balance'] = 40.00 
print(fg)

for index, row in fg.iterrows():
    if index==0:
        continue
    if fg.loc[index,'Status']=='Deposit':
        print("reached here")
        fg.at[float(index),'Balance']=sum(fg.loc[float(index),'Amount'],fg.loc[float(index-1),'Balance'])
    elif fg.loc[index,'Status']=='withdraw':  
        fg.at[float(index),'Balance']=fg.loc[float(index),'Amount']-fg.loc[float(index-1),'Balance']

    print(fg)

1 个答案:

答案 0 :(得分:1)

IIUC,np.wherecumsum

df['Balance'] = np.where(df['Status'].eq('Deposit'),df['Amount'], df['Amount'] * -1)

df['Balance'] = df['Balance'].cumsum()

         Date    Status  Amount  Balance
0  06-10-2000   Deposit    40.0     40.0
1  09-12-2002  Withdraw  1000.0   -960.0
2  27-06-2001   Deposit    47.0   -913.0
3  07-12-2021  Withdraw   100.0  -1013.0
4  06-10-2022   Deposit   120.0   -893.0
5  06-10-2000   Deposit    40.0   -853.0
6  09-12-2024  Withdraw    50.0   -903.0