How to insert a pandas dataframe to an already existing table in a database?

make use of if_exists parameter:

df.to_sql('db_table2', engine, if_exists="replace")

or

df.to_sql('db_table2', engine, if_exists="append")

from docstring:

"""
if_exists : {'fail', 'replace', 'append'}, default 'fail'
    - fail: If table exists, do nothing.
    - replace: If table exists, drop it, recreate it, and insert data.
    - append: If table exists, insert data. Create if does not exist.
"""

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)