UserYmY
UserYmY

Reputation: 8554

Python pandas: Find a value in another dataframe and replace it

I have two dataframes df_l (with 3000 rows) and df_s(with 100 rows) : df_l

version|update_date
2.3.4| date1
3.4.5|date2

and df_s

version|release_date
    2.3.4| date1
    3.4.5|date2
    3.3.3|date3

I want to check if a version in df_l is in df_s, then I want to update the values in df_l.update_date to df_s.release_date. Here is my code

df_l.ix[df_l['version'].isin(df_s['version']),'update_date'] = df_s['release_date']

but the updated values in df_l.update_date are wrong I am guessing that the matching is not taking place correctly. Can anybody help?

Upvotes: 2

Views: 563

Answers (1)

jezrael
jezrael

Reputation: 862611

IIUC you need merge with inner join how='inner' which is by default. Also you can omit on if in both DataFrames are only 2 columns and one of them is same in both:

print (pd.merge(df_l, df_s))
  version update_date release_date
0   2.3.4       date1        date1
1   3.4.5       date2        date2

Upvotes: 1

Related Questions