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 :
import pandas as pd df1 = pd.read_csv(xxx) df2 = pd.read_csv(xxx) df1["Enrich"] = "" for index_1 , row_1 in df1.iterrows(): for index_2,row_2 in df2.iterrows(): if df1.loc[index_1,"filter_1"] == df2.loc[index_2,"filter_2"]: df1.loc[index_1,"Enrich"] == df2.loc[index_2,"Theneededvalue"]
Thanks for your help !
JaNa
Advertisement
Answer
Without a sample it’s hard to help but vlookup can be implemented like that:
pd.merge(df1, df2, left_on='filter1', right_on='filter2', how='left')