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
没有示例很难提供帮助,但可以像这样实现 vlookup:
pd.merge(df1, df2, left_on='filter1', right_on='filter2', how='left')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.