Skip to content

Instantly share code, notes, and snippets.

View jackiep00's full-sized avatar

Jack Yin jackiep00

View GitHub Profile
@gordthompson
gordthompson / postgresql_df_upsert.py
Last active April 12, 2024 20:09
Build a PostgreSQL INSERT … ON CONFLICT statement and upsert a DataFrame
# version 1.2 - 2022-10-04
import pandas as pd
import sqlalchemy as sa
def df_upsert(data_frame, table_name, engine, schema=None, match_columns=None):
"""
Perform an "upsert" on a PostgreSQL table from a DataFrame.
Constructs an INSERT … ON CONFLICT statement, uploads the DataFrame to a