简体   繁体   中英

How to merge two DataFrames using specific conditions in Python Pandas?

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?

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

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