Rico Yu
Rico Yu

Reputation: 29

Vlookup/Map value from one dataframe to another dataframe in Python

I want to do something similar to the vlookup in the python. Here is a dataframe I want to lookup for value 'Flow_Rate_Lupa'enter image description here

And here is the dataframe I want to fill the data by looking at the same month+day to fill the missing value. Is there any one to help me to solve how to do this QAQenter image description here

Upvotes: 0

Views: 906

Answers (2)

Lara Amro
Lara Amro

Reputation: 68

I usually merge the two data frames and define an indicator, then filter out the values where the indicator says both meaning data is in both data frames.

import pandas as pd

mergedData= pd.merge(df1,df2, how='left' ,left_on='Key1', right_on='Key2', indicator ='Exists')
filteredData = mergedData[mergedData[Exists]='both']

Upvotes: 2

jezrael
jezrael

Reputation: 862611

Use DataFrame.merge with left join and the nreplace missing values by Series.fillna with DataFrame.pop for use and remove column:

df = df2.merge(df1, on=['month','day'], how='left', suffixes=('','_'))
df['Flow_Rate_Lupa'] = df['Flow_Rate_Lupa'].fillna(df.pop('Flow_Rate_Lupa_'))

Upvotes: 1

Related Questions