yanadm
yanadm

Reputation: 707

Fill a column in the dataframe based on similar values from another dataframe in pandas

I have two dataframe:

   df1                             df2
 №    year                       №    year
 1    2010                      373     
 2    2010                      374
 3    2010                      375
 4    2010                      376
 5    2010                      ...                 
...
372   2017
373   2017
374   2017
375   2017
376   2017
377   2017
...                           
899   2026
900   2026
901   2026

I need to find all the values from column "№" of the df2 in the df1 and fill the column "year" in the df2 with values from the df1. The result should look like this:

   df2
 №    year
373   2017
374   2017
375   2017
376   2017
...

I tried to do it this way

df2['year'] = np.where(df2['№'] == df1['№'] , 'Insert value from df1['year'], '0')

I first tried to insert '1', instead of a year, to check if the code is working, it got me such an error

ValueError: Can only compare identically-labeled Series objects

Any suggestion, please?

Upvotes: 2

Views: 416

Answers (1)

jezrael
jezrael

Reputation: 862691

I think need map by Series created by set_index - if some value not match get NaNs:

df2['year'] = df2['№'].map(df1.set_index('№')['year'])

If need replace NaNs to original values:

df2['year'] = df2['№'].map(df1.set_index('№')['year']).combine_first(df2['year'])

Upvotes: 2

Related Questions