user14473305
user14473305

Reputation: 57

Given 2 data frames search for matching value and return value in second data frame

Given 2 data frames like the link example, I need to add to df1 the "index income" from df2. I need to search by the df1 combined key in df2 and if there is a match return the value into a new column in df1. There is not an equal number of instances in df1 and df2 and there are about 700 rows in df1 1000 rows in df2.

I was able to do this in excel with a vlookup but I am trying to apply it to python code now.

Data Frame images

Upvotes: 0

Views: 186

Answers (2)

user14473305
user14473305

Reputation: 57

https://www.geeksforgeeks.org/how-to-do-a-vlookup-in-python-using-pandas/

Here is an answer using joins. I modified my df2 to only include useful columns then used pandas left join.

Left_join = pd.merge(df,  
                     zip_df,  
                     on ='State County',  
                     how ='left') 

Upvotes: 0

Piyush Sambhi
Piyush Sambhi

Reputation: 883

This should solve your issue:

df1.merge(df2, how='left', on='combind_key')

This (left join) will give you all the records of df1 and matching records from df2.

Upvotes: 1

Related Questions