Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
951 B
2
Indexable
Never
# Assuming you have source_df and target_df loaded

# Iterate over the primary key values in source_df
for pk_value in source_df[pk].unique():
    source_subset = source_df[source_df[pk] == pk_value]
    target_subset = target_df[target_df[pk] == pk_value]

    if pk_value not in target_subset[pk].values:
        # Insert the entire row from source_df, including the primary key
        insert_data = source_subset.copy()
        insert_data.to_sql(table_name, connection_ext, if_exists='append', index=False)

    else:
        # Check for updates and generate and execute UPDATE query if needed
        mask = (source_subset != target_subset).any(axis=1)
        if mask.any():
            updates = source_subset.loc[mask].copy()
            for column_name, source_val in updates.items():
                target_df.loc[target_df[pk] == pk_value, column_name] = source_val

# Commit changes to the database (if necessary)
connection_ext.commit()