Update existing row in database from pandas df

前端 未结 1 771
情书的邮戳
情书的邮戳 2020-12-29 04:34

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 exis

相关标签:
1条回答
  • 2020-12-29 05:15

    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()
    
    0 讨论(0)
提交回复
热议问题