次のデータフレームがあります。
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)
IIUC、np.where
およびcumsum
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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加