Rakesh Sharma
Rakesh Sharma

Reputation: 11

Update a DF in Pandas based on condition from different DF

I have df1

index   state   fin_salary  new_title
5         CA      1          Data Scientist
8         CA      1          Data Scientist
35        CA    150000       Deep Learning Engineer
36        CA    1            Data Analyst
39        CA    1            Data Engineer
43        CA    1            Data Scientist
56        CA    1            Data Scientist

And another datfarame df2

state   new_title                     fin_salary
CA  Artificial Intelligence Engineer    207500.0
CA  Data Analyst                       64729.0
CA  Data Engineer                      146000.0
CA  Data Scientist                     129092.75
CA  Deep Learning Engineer             162500.0
CA  Machine Learning Engineer          133120.0
CA  Python Developer                   96797.0

So I want to update df1 with fin_salary from df2 based on condition state and new_title and where fin_salary = 1. So my desired output should be

index   state   fin_salary           new_title
5         CA      129092.75         Data Scientist
8         CA      129092.75         Data Scientist
35        CA    150000              Deep Learning Engineer
36        CA    64729.0             Data Analyst
39        CA    146000.0             Data Engineer
43        CA    129092.75            Data Scientist
56        CA    129092.75            Data Scientist

Upvotes: 1

Views: 23

Answers (1)

You can do this:

DF = df1.merge(df2, on=['state','new_title'], how = 'inner')

df_final = DF[DF['fin_salary']==1]

Upvotes: 1

Related Questions