python merge using or statement

Solutions on MaxInterview for python merge using or statement by the best coders in the world

showing results for - "python merge using or statement"
Hannes
01 Nov 2020
1dupes = (df.B_on_A_match_1 == df.B_on_A_match_2) # also could remove A_on_B_match
2df.loc[~dupes]
3
4     A  A_on_B_match_1  A_on_B_match_2    B  B_on_A_match_1  B_on_A_match_2
50  1.0             NaN             NaN  NaN             9.0             4.0
60  NaN             2.0             6.0  8.0             NaN             NaN
71  NaN             4.0             4.0  5.0             NaN             NaN
Liah
17 Oct 2019
1suff_A = ['_on_A_match_1', '_on_A_match_2']
2suff_B = ['_on_B_match_1', '_on_B_match_2']
3
4pd.concat([df1.merge(df2, on='A', suffixes=suff_A), 
5           df1.merge(df2, on='B', suffixes=suff_B)])
6
7     A  A_on_B_match_1  A_on_B_match_2    B  B_on_A_match_1  B_on_A_match_2
80  1.0             NaN             NaN  NaN             9.0             4.0
91  4.0             NaN             NaN  NaN             5.0             5.0
100  NaN             2.0             6.0  8.0             NaN             NaN
111  NaN             4.0             4.0  5.0             NaN             NaN