I have theses two dfs
df1 = pd.DataFrame({'pupil': ["sarah", "john", "fred"],
'class': ["1a", "1a", "1a"]})
df2 = pd.DataFrame({'pupil_mixed': ["sarah", "john", "lex"],
'class': ["1a", "1c", "1a"]})
I want to append the row values from the column "pupil_mixed" from df2 to the column "pupil" in df1 if the values are no duplicates
desired outcome:
df1 = pd.DataFrame({'pupil': ["sarah", "john", "fred", 'lex'],
'class': ["1a", "1a", "1a", NaN]})
I used append with loc
df1 = df1.append(df2.loc[df2['pupil_mixed'] != df1['pupil'] ])
which just appended the other column to the df with the matching row value and changed the non matching row values to NaN
pupil class pupil_mixed
0 sarah 1a NaN
1 john 1a NaN
2 fred 1a NaN
2 NaN 1a lex