Use merge with left join and parameter suffixes, lastr remove original columns email address and phone number (with _):
df1 = pd.DataFrame({ 'username':list('abccdd'), 'email address':[''] * 6, 'phone number':[''] * 6, 'duration':[5,3,6,9,2,4], }) print (df1) username email address phone number duration 0 a 5 1 b 3 2 c 6 3 c 9 4 d 2 5 d 4 df2 = pd.DataFrame({ 'username':list('abcd'), 'email address':['[email protected]','[email protected]','[email protected]','[email protected]'], 'phone number':range(4) }) print (df2) username email address phone number 0 a [email protected] 0 1 b [email protected] 1 2 c [email protected] 2 3 d [email protected] 3
df = (df1.merge(df2, on='username', how='left', suffixes=('_','')) .drop(['email address_','phone number_'], axis=1) .reindex(columns=df1.columns)) print (df) username email address phone number duration 0 a [email protected] 0 5 1 b [email protected] 1 3 2 c [email protected] 2 6 3 c [email protected] 2 9 4 d [email protected] 3 2 5 d [email protected] 3 4
Another solution with difference for all columns names without defined in list and reindex for same ordering like in df1 of columns:
c = df1.columns.difference(['email address','phone number']) df = df1[c].merge(df2, on='username', how='left').reindex(columns=df1.columns) print (df) username email address phone number duration 0 a [email protected] 0 5 1 b [email protected] 1 3 2 c [email protected] 2 6 3 c [email protected] 2 9 4 d [email protected] 3 2 5 d [email protected] 3 4