1 回答

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊
我認(rèn)為您可以拆分代碼以使用聚合進(jìn)行創(chuàng)建,然后重新設(shè)計(jì)由累積總和填充的列:DataFramesumamount
df = pd.read_csv('payments.csv', index_col=0)
df['effective_date'] = pd.to_datetime(df['effective_date'])
df = df.groupby(['account_id', 'currency', 'effective_date'], as_index=False).sum()
df['amount'] = df.groupby('account_id')['amount'].cumsum()
print (df)
account_id currency effective_date amount
0 1 USD 2019-12-26 50
1 1 USD 2019-12-27 120
2 2 USD 2019-11-06 90
3 2 USD 2019-11-07 120
df.to_csv('cumulativePayments.csv')
另一個(gè)想法應(yīng)該是將第一列轉(zhuǎn)換為日期時(shí)間,并且對(duì)于寫(xiě)入文件,請(qǐng)刪除默認(rèn)索引值:
df = pd.read_csv('payments.csv', parse_dates=[0])
df = df.groupby(['account_id', 'currency', 'effective_date'], as_index=False).sum()
df['amount'] = df.groupby('account_id')['amount'].cumsum()
df.to_csv('cumulativePayments.csv', index=False)
添加回答
舉報(bào)