0

Guys I have 2 dataframes df and population. df

population

I simply want to add new column named Population to df and to add population numbers from dataframe population to this column. I want to have population data next to each city.

How can I manage this ? Any advices?

Ideal Solution

Product Province    Quantity  Population
PRODUCT_A   ankara  16          5663322
PRODUCT_A   ankara  25          5663322
PRODUCT_A   ankara  56          5663322
PRODUCT_A   ankara  16          5663322
PRODUCT_A   adana   11          2258718
PRODUCT_A   adana   25          2258718
PRODUCT_A   ankara  35          5663322
PRODUCT_A   adana   54          2258718
PRODUCT_A   adana   17          2258718
PRODUCT_A   adana   30          2258718
PRODUCT_A   adana   12          2258718
PRODUCT_A   ankara  18          5663322
PRODUCT_A   ankara  14          5663322
PRODUCT_A   ankara  21          5663322
PRODUCT_A   aydin   16          1119084
cugara
  • 23
  • 1
  • 3

1 Answers1

2

This is done via a join operation which in pandas can be done with .merge().

Kindly try using the following:

df = df.merge(population,how='left',on='Province')

Also please consider reading the following answer for a detailed guide on joins and merges

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
  • In the future, please consider carefully whether it is actually necessary to post an answer when we have a canonical Q&A covering the subject already. It is probably better to just vote to close the question as a duplicate. This really helps in keeping the site clean and reducing the duplication of information. Thanks! – Cody Gray - on strike Nov 18 '21 at 20:15