0

I have two dataframes df1 and df2 with different number of rows (say N and M). df1 and df2 have also different columns, but one columns reports the same quantity (e.g., city name).

Example df1:

Population   Size   City
1020211      300     A
 209111      121     B
...          ...     ...

Example df2:

Date       Col1     Col2     City 
01-01-2010 21291    323828    A
02-03-2011 48483    233111    A
01-04-2009 455722   4822      B
...

I need to create a new column in dataframe df2, so that for each City, I pick 'Population' from the matching City in df1 and copy its value in df2 as df2['Population']. So df2 should look like this:

Date       Col1     Col2     City  Population
01-01-2010 21291    323828    A    1020211
02-03-2011 48483    233111    A    1020211 
01-04-2009 455722   4822      B     209111
...

Is there a 'pythonic' way of doing this without resorting to for loops?

user12394113
  • 331
  • 3
  • 9

0 Answers0