I have a dataframe that looks like this.
name id
0 alec NaN
1 cole NaN
2 troy 1.0
And I have a postgres db that reflects the same structure.
I want to insert this dataframe into my database, but I know that row at index 2 is already in the db because it has an 'id'. So in theory, I only want to insert rows at index 0 and 1, because I know that they are not yet in the database.
Then I want to get back the same dataframe with the new ids updated into my dataframe so it looks like this.
name id
0 alec 2.0
1 cole 3.0
2 troy 1.0
I am using psycopg2
and a postgresql
database. I am also inserting thousands of records at a time, so I need this execution to be relatively fast.
Is there an easy way to do this?