我在SQLServer上有一个类似这样的表,其中每一行都有一个唯一的Event A
和Event B
组合
`Global Rules Table
ID Event 1 | Event 2 | Validated as | Generated as | Generated with score
1 EA1 EB1 Rule Anti-Rule 0.01
2 EA1 EB2 Rule Rule 0.95
3 ... ... ... ... ...
我有另一个表,它对Global Rules Table
具有外键约束,称为Local Rules Table
我有一个熊猫数据框,看起来像这样
Event 1 | Event 2 | Validated as | Generated as | Generated with score
EA1 EB1 Rule Rule 0.85
EA1 EB2 Rule Rule 0.95
... ... ... ... ...
因为我在Local Rules
和Global Rules
表之间有这个外键约束,所以我不能使用df.to_sql('Global Rules',con,if_exists='replace')
我想根据dataframe中的值在数据库中更新的列是Generated as
和Generated with score
,那么,根据我拥有的dataframe只更新数据库表中的这些列的最佳方法是什么呢?是否有一些现成的函数或库我不知道
我还没有找到一个图书馆来完成这项工作。我开始自己写一个,在PyPi上主持,但还没有完成
在这种情况下,针对SQL临时表的内部联接可以很好地工作。它将只更新SQL中的一个子集列,并且对于更新许多记录来说非常有效
我假设您正在使用pyodbc连接到SQL server
SQL游标
创建临时表
将DataFrame插入到临时表中
从临时表更新主表中的值
放下临时桌子
相关问题 更多 >
编程相关推荐