如何以多列组合为主键从另一CSV更新system.csv(Python实现)
问题需求:基于CSV主键更新累计数据
现有两个表头一致的CSV文件input.csv和system.csv,表头为:ID,Date,time,type,status,member,account,property,credit quantity, debit quantity, Net quantity, credit value, debit value, Net value, currency
其中input.csv是单次运行的输入数据,system.csv作为累计数据的"数据库",需要基于input.csv进行更新,规则如下:
- 以
Date、member、account、property的组合作为主键 - 若主键在
system.csv中已存在:- 将
input.csv对应行的credit quantity、debit quantity、Net quantity、credit value、debit value、Net value字段值累加到system.csv的对应字段 - 同时更新
time等非累加字段(参考下方示例数据)
- 将
示例数据
input.csv
ID,Date,time,type,status,member,account,property,credit quantity, debit quantity, Net quantity, credit value, debit value, Net value, currency id01,2023.03.16,21:00:00,visa,active,xyz,acc001,cc,100, 0, 1000, 100, 0, 1000, usd id02,2023.03.16,22:00:00,visa,active,abc,acc002,cc,0,200, 2000, 0, 200, 2000, usd
system.csv
ID,Date,time,type,status,member,account,property,credit quantity, debit quantity, Net quantity, credit value, debit value, Net value, currency id101,2023.03.16,08:00:00,visa,active,xyz,acc001,cc,500, 0, 5000, 400, 0, 4000, usd id102,2023.03.16,09:00:00,visa,active,abc,acc002,cc,0,600, 6000, 0, 200, 2000, usd
更新后的system.csv
ID,Date,time,type,status,member,account,property,credit quantity, debit quantity, Net quantity, credit value, debit value, Net value, currency id101,2023.03.16,21:00:00,visa,active,xyz,acc001,cc,600, 0, 6000, 500, 0, 5000, usd id102,2023.03.16,22:00:00,visa,active,abc,acc002,cc,0,800, 8000, 0, 400, 4000, usd
当前进度
已将两个CSV文件读取转换为Pandas DataFrame,但不清楚如何实现上述更新逻辑,请求帮助。
内容的提问来源于stack exchange,提问作者Indi
相关产品推荐
相关产品推荐

