Update existing row in database from pandas df Update existing row in database from pandas df postgresql postgresql

Update existing row in database from pandas df


One way is to make use of an sqlalchemy "table class" and session.merge(row), session.commit():

Here is an example:

for row in range(0, len(df)):    row_data = table_class(column_1=df.ix[i]['column_name'],                           column_2=df.ix[i]['column_name'],                           ...                           )    session.merge(row_data)    session.commit()


For sql alchemy case of read table as df, change df, then update table values based on df, I found the df.to_sql to work with name=<table_name> index=False if_exists='replace'

This should replace the old values in the table with the ones you changed in the df