0

I need to update a pandas data frame column using column values in another data frame. I am new to Python but have some background in SQL.

Specifically, I have 'city' and 'state columns in df1 with 62K rows, and 'city' and 'state' columns in df2 (a data frame with population data from the US Census). Do I have to loop through df1 to update the empty 'population' column for each row, or can it be done with a merge, update, or replace in just a few lines of code?

In SQL I would simply write an update query to update the empty table column with the values in the column in the other table after joining the two tables with a left join. Is there something similar to that in Python? Thank you very much.

MrShakila
  • 874
  • 1
  • 4
  • 19
Pozinho
  • 1
  • 1
  • You can perform a left merge on those two columns, if not all data is present in the second DataFrame, also add `combine_first`/`fillna` on the merged data to fill original values – mozway Oct 17 '22 at 03:45

0 Answers0