1

I have dataframes:

df1:
| |A|B|C|D|E|
|0|1|2|3|4|5|
|1|1|3|4|5|0|
|2|3|1|2|3|5|
|3|2|3|1|2|6|
|4|2|5|1|2|3|

df2:
| |K|L|M|N|
|0|1|3|4|2|
|1|1|2|5|3|
|2|3|2|3|1|
|3|1|4|5|0|
|4|2|2|3|6|
|5|2|1|2|7|

What I need to do is match column A of df1 with column k of df2; column C of df1 with L of df2; and column D of df1 with column M of df2. If the values are matched the corresponding value of N in df2 should be assigned to a new column F in df1. The output should be:

| |A|B|C|D|E|F|
|0|1|2|3|4|5|2|
|1|1|3|4|5|0|0|
|2|3|1|2|3|5|1|
|3|2|3|1|2|6|7|
|4|2|5|1|2|3|7|
abiral112
  • 13
  • 4

2 Answers2

4

Use DataFrame.merge with left join and rename columns for match:

df = df1.merge(df2.rename(columns={'K':'A','L':'C','M':'D', 'N':'F'}), how='left')
print (df)
   A  B  C  D  E  F
0  1  2  3  4  5  2
1  1  3  4  5  0  0
2  3  1  2  3  5  1
3  2  3  1  2  6  7
4  2  5  1  2  3  7
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1
df3 = df1.join(df2)
F = []
for _, row in df3.iterrows():
    if row['A'] == row['K'] and row['C'] == row['L'] and row['D'] == row['M']:
        F.append(row['N'])
    else:
        F.append(0)
df1['F'] = F
df1
MusHusKat
  • 438
  • 2
  • 9
  • 1
    https://stackoverflow.com/questions/24870953/does-pandas-iterrows-have-performance-issues/24871316#24871316 – jezrael Feb 05 '21 at 07:00