Search code examples
pythonpandasdataframedata-sciencedata-cleaning

How to add new column from another dataframe based on values in column of first dataframe?


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

Solution

  • 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