python - 如何添加/计算 'Balance' 行?

标签 python pandas

我从银行下载了交易,例如

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())

我现在的问题是,是否需要舍入?可以优化或以更好的方式编写吗?

谢谢!

最佳答案

感谢@meW我没想到cumsum()

这就是我能做的

%%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 - 如何添加/计算 'Balance' 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53994331/

相关文章:

python - 检测图像上其他物体上附着的小方 block

python - Pandas groupby : Count the number of occurrences within a time range for each group

python - DataFrame Pandas 显示 NAN

python - 我如何抵消 Pandas dayofyear 以便开始日期是 10 月 1 日而不是 1 月 1 日?

python - 循环读取文件中的特定行(优化)

python - __setattr__ 与类属性设置属性

python - $.ajax 函数 : send json data : parse at serverside function

python - 如何通过代码清除 Google Colab 中的输出?

python - 如何加速 pandas 数据帧迭代

python - 计算矩阵与其转置的相关性