I have a PostgreSQL db. Pandas has a 'to_sql' function to write the records of a dataframe into a database. But I haven't found any documentation on how to update an existing database row using pandas when im finished with the dataframe.
Currently I am able to read a database table into a dataframe using pandas read_sql_table. I then work with the data as necessary. However I haven't been able to figure out how to write that dataframe back into the database to update the original rows.
I dont want to have to overwrite the whole table. I just need to update the rows that were originally selected.
to_sqldoes not support updates. The best approach I have found so far is to create anON INSERTtrigger in the database table, that updates all fields if inserting a duplicate primary key.original_tabledataframe, calloriginal_table.update(modified_table)wheremodified_tableis your dataframe, and then...to_sql(if_exists='replace')with this new dataframe object.