Home > Software design >  How to add new column from another dataframe based on values in column of first dataframe?
How to add new column from another dataframe based on values in column of first dataframe?

Time:11-15

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

CodePudding user response:

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

CodePudding user response:

use join in dataframe. use this link

  • Related