pandas 更新mysql,如何使用Pandas DataFrame对数据库表的现有行执行UPDATE?

该博客探讨了如何从MySQL数据库中查询数据,将其加载到Pandas DataFrame中进行修改,然后将更新的行写回相同的数据库表,而不必将整个表拉取并替换。作者考虑了数据量和需要更新的行数,并提供了一个解决方案,涉及创建临时表,删除要更新的行,然后重新插入修改后的行。
摘要由CSDN通过智能技术生成

I am attempting to query a subset of a MySql database table, feed the results into a Pandas DataFrame, alter some data, and then write the updated rows back to the same table. My table size is ~1MM rows, and the number of rows I will be altering will be relatively small (<50,000) so bringing back the entire table and performing a df.to_sql(tablename,engine, if_exists='replace') isn't a viable option. Is there a straightforward way to UPDATE the rows that have been altered without iterating over every row in the DataFrame?

I am aware of this project, which attempts to simulate an "upsert" workflow, but it seems it only accomplishes the task of inserting new non-duplicate rows rather than updating parts of existing rows:

Here is a skeleton of what I'm attempting to accomplish on a much larger scale:

import pandas as pd

from sqlalchemy import

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值