我有一个表,其中我的basedata作为postgresql表,让它为'basedata':
select d.id,min(start),max(end),sum(isnull(t.col1,0.00))
from DateRange d
left join transactions t on 1=1 and t.dateime between d.start and d.end
group by d.id
这是一个主键为'id'的postgresql表。
现在我将收到一个熊猫数据帧,其中包含有关各自人员的新数据或更新数据,例如:
basedata:
id,name,age,height
1,john,17,185
2,nick,24,174
3,sarah,19,165
逻辑选择应该是:
new_data:
id,height
17,harry,26,177
23,mary,14,145
2,25,165
结果应为:
new id -> insert into database
id already exists -> do nothing if every field is the same (like for sarah)
id already exists -> update differing fields
我正在努力以最佳方式使用basedata:
id,165
17,145
和python
。
我需要遍历DataFrame并针对数据库检查每个数据行,还是有更优雅的方法来做到这一点?以及如何遍历数据框的最佳方式?