0
votes

I have two Data Frames:

DataFrame 1

df1 = pd.DataFrame()
df1["ID1"] = [np.nan, 1, np.nan, 3]
df1["ID2"] =[np.nan, np.nan , 2, 3]
df1

DataFrame 2

df2 = pd.DataFrame()
df2["ID"] = [1, 2, 3, 4]
df2

And I need to merge these two DataFrames using below conditions:

  1. If in df1 ID1 == ID2 then I can merge df1 with df2 using df1.ID1 = df2.ID or df1.ID2 = df2.ID
  2. If in df1 ID1 != ID2 then I have to mergre df1 with df2 using both mentioned in point 1 conditions means: df1.ID1 = df2.ID and df1.ID2 = df2.ID

I have the command as above in points 1 and 2, nevertheless I totaly do not know how to write it in Python Pandas, any suggestions ?

1
Could you write a sample data you expect for both cases? - Carmoreno

1 Answers

0
votes

if I understood correctly, this will fix your problem

df1 = pd.DataFrame()
df1["ID1"] = [np.nan, 1, np.nan, 3]
df1["ID2"] =[np.nan, np.nan , 2, 3]
df2 = pd.DataFrame()
df2["ID"] = [1, 2, 3, 4]

if df1["ID1"].equals(df1["ID2"]) == True:
  pass  #do your merging here 
else:
  df1["ID1"],df1["ID2"] = df2["ID"],df2["ID"]

df1

output:

     ID1  ID2
 0    1    1
 1    2    2
 2    3    3
 3    4    4