I would like to enrich a dataframe (df1) from another(df2) by adding a new column in df1 and enriching it based on what I find in df2. The size of the 2 df is different as well as the name of the columns. I would like to do like a Vlookup function in Excel.
This what I’ve done but I think there is a way to optimize it :
JavaScript
x
9
1
import pandas as pd
2
df1 = pd.read_csv(xxx)
3
df2 = pd.read_csv(xxx)
4
df1["Enrich"] = ""
5
for index_1 , row_1 in df1.iterrows():
6
for index_2,row_2 in df2.iterrows():
7
if df1.loc[index_1,"filter_1"] == df2.loc[index_2,"filter_2"]:
8
df1.loc[index_1,"Enrich"] == df2.loc[index_2,"Theneededvalue"]
9
Thanks for your help !
JaNa
Advertisement
Answer
Without a sample it’s hard to help but vlookup can be implemented like that:
JavaScript
1
2
1
pd.merge(df1, df2, left_on='filter1', right_on='filter2', how='left')
2