我可以从我的银行下载交易,例如
Date, Amount
不幸的是,CSV下载不包含期初余额,因此我将初始值添加到了DataFrame的顶部。所以现在数据看起来像:
Date, Amount, Balance
2018-01-01, 0, 10
2018-01-01, 10, 20
2018-01-02, 20, 40
2018-01-02, -10, 30
2018-01-03, 20, 50
2018-01-31, 0, 50
通过将以前的余额加到当前金额来计算余额。
这是我可以召集的,它闻起来很不好:
df = pd.read_csv("~/Downloads/Chequing.CSV", parse_dates=[0], na_values="n/a")
df['Date'] = pd.to_datetime(df['Date'])
df['Balance'] = 0
df1 = pd.DataFrame(data={'Date': ['2018-01-01'], 'Transaction':
['CREDIT'], 'Name': ['Open'], 'Memo': ['Open'], 'Amount': [0], "Balance": [10.00]})
df1['Date'] = pd.to_datetime(df1['Date'])
df2 = pd.concat([df1, df], sort=False, ignore_index=True)
for i in range(1, len(df2)):
prev_balance = df2['Balance'].iloc[i-1]
amount = df2['Amount'].iloc[i]
new_balance = round(amount + prev_balance, 2)
df2['Balance'].iloc[i] = new_balance
# Above generates a warning:
# SettingWithCopyWarning:
# A value is trying to be set on a copy of a slice from a DataFrame
# While writing this, I was able to get it working by replacing the for loop above with:
df2['Balance'] = round((df2["Amount"] + df2["Balance"]).cumsum(), 2)
pd.set_option('display.max_columns', None)
print(df2.groupby(df['Date'].dt.strftime('%m %B'))['Date', 'Amount', 'Transaction', 'Name', 'Balance'].max())
我的问题现在变成,是否需要四舍五入?是否可以优化或以更好的方式编写?
谢谢!
最佳答案
%%time
df.Balance = np.concatenate((df.Balance[:1], (df.Balance.shift().fillna(0)+df.Amount).cumsum()[1:]))
#Wall time: 2 ms
与for循环方法比较
%%time
for i in range(1,len(df.Balance)):
df.Balance[i] = df.Balance[i-1]+df.Amount[i]
# Wall time: 173 ms
每月最大余额
df
Date Amount Balance
0 2018-01-01 0 10
1 2018-01-01 10 20
2 2018-01-02 20 40
3 2018-02-02 -10 30
4 2018-03-03 20 50
5 2018-03-31 10 60
df.groupby(df.Date.dt.month).apply(lambda x: x[x.Balance == x.Balance.max()]).reset_index(drop=True)
Date Amount Balance
0 2018-01-02 20 40
1 2018-02-02 -10 30
2 2018-03-31 10 60
希望对您有所帮助。欢迎评论;)
关于python - 如何添加/计算“余额”行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53994331/