Skip to content
Advertisement

DataFrame comparison with SQL Server table and upload just the differences

I have an SQL table (table_1) that contains data, and I have a Python script that reads a csf and creates a dataframe.

I want to compare the dataframe with the SQL table data and then insert the missing data from the dataframe into the SQL table.

I went around and read this comparing pandas dataframe with sqlite table via sqlquery post and Compare pandas dataframe columns to sql table dataframe columns, but was not able to do it.

The table and the dataframe have the exact same columns.

The dataframe is:

JavaScript

and the SQL table (using SQLAlchemy):

JavaScript

I’d like to compare the df to the SQL table and insert userid 3, Alice, with all her details and it’s the only value missing between them.

Advertisement

Answer

Since you are only interested in inserting new records, and are loading from a CSV so you will have data in local memory already:

JavaScript

Other options would require first loading more data into SQL than needed.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement