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:
df1.ID1 = df2.ID
or df1.ID2 = df2.ID
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.