简体   繁体   中英

How do I update some values of my target column in a dataframe by matching its multiple columns with one column in another dataframe in python?

I have two pandas dataframes:

  • df1
  • df2

df1 looks like this:

a     f1     f2     f3     target
1     aa     bb     cc     NaN
2     xx     yy     zz     NaN
more rows....

df2 looks like this:

b     f1     target
3     bb     450
4     xx     220
more rows....

The desired output for df1 is this:

a     f1     f2     f3     target
1     aa     bb     cc     450
2     xx     yy     zz     220
more rows....

This is my question:

How do I update df1["target"] with values from df2["target"] by matching df2["f1"] with df1["f1"] or df1["f2"] or df1["f3"] .

The merge and update functions don't work for me because I need to match multiple columns. Other solutions I saw were also for matching ALL multiple columns, which is not the case for my data. (I only need to match one column with ANY multiple columns.)

Thanks.

one approach would be to create a dictionary and apply it to first column if possible, second if possible etc.

d = dict(zip(df2.f1.values,df2.target.values))
df1.target = df1.apply(lambda r: d.get(r['f1'],d.get(r['f2'],d.get(r['f3']))), axis=1)

Consider iteratively merging into an appended dataframe that merges to original one:

# PAIRWISE MERGE FIELDS
merge_cols = [[i, 'f1'] for i in list(df1.columns[1:len(df1.columns)-1])]
# LIST OF INNER JOIN MERGES
dfs = [pd.merge(df1, df2, left_on=m[0], right_on=m[1], how='inner') for m in merge_cols]

# APPEND DFS
stackdf = pd.concat(dfs)[['a','target_y']]
finaldf = stackdf.merge(df1, on='a')\
                 .assign(target=stackdf['target_y'].values)\
                 .drop(['target_y'], axis=1)

print(finaldf)
#    a  f1  f2  f3  target
# 0  2  xx  yy  zz     220
# 1  1  aa  bb  cc     450

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM