You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何以多列组合为主键从另一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中已存在:
    1. 将input.csv对应行的credit quantity、debit quantity、Net quantity、credit value、debit value、Net value字段值累加到system.csv的对应字段
    2. 同时更新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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 15:07:49