0

I have the following dataframes:

df1

index  | colA  | colB
0          a       1
1          b       2
2          a       9

df2

index  | colA  | colB
5          a       3
8          c       4

I want to update df1 based on df2, meaning for all rows "i" such that

df1['ColA'].iloc[i] == df2['colB'].iloc[i]

The index do not match so .update() does not work here.

Desired output:

df1

index  | colA  | colB
0          a       3
1          b       2
2          a       3
Asddfg
  • 237
  • 3
  • 13

1 Answers1

0

You can do with boolean indexing:

idx = df1['colA'].values == df2['colA'].values
df1.loc[idx, 'colB'] = df2.loc[idx,'colB'].values 
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74