I have 2 dataframes outputted from my python script: dt1
& dt2
Each dataframe has 4 columns (colA
, colB
, colC
, & colD
) and each dataframe has a different length (dt1
always has more rows)
I want to create a 3rd dataframe dt3
that:
- copies
dt1
- adds
dt2[colD]
value to the corresponding row IFdt1[colA]
=dt2[colA]
&dt1[colB]
=dt2[colB]
&dt1[colC]
=dt2[colC]
, if the 3 columns don't match I want to load null
I've tried iterating through using a for loop with combined if else statements but can't seem to get the check piece right, please help!
CodePudding user response:
Use merge
from pandas
df1.merge(df2[["colA","colB","colC","colD"]],on=["colA","colB","colC"])
Updated as Op req.
df1.colD.fillna('---')