My Dataframe looks like following.I am using Pandas merge function to merge two dataframes, and I am trying to find row that was dropped. Is there a way in Pandas or python
merge = pd.merge(df1,df2,on='Name', indicator=True, how='outer')
print (merge)
#drop dataframe
del df1
del df2
Use merge with outer join and parameter indicator=True
:
df = pd.merge(df1,df2,on='Name', indicator=True, how='outer')
print (df)
Name Age Add _merge
0 A 34.0 rt both
1 B 23.0 ct both
2 C 90.0 NaN left_only
3 D NaN pt right_only
Last filter no both rows by boolean indexing:
print (df[df['_merge'] != 'both'])
Name Age Add _merge
2 C 90.0 NaN left_only
3 D NaN pt right_only
Another solution is filtering with isin and inverting mask by ~
:
print (df1[~df1['Name'].isin(df2['Name'])])
Name Age
2 C 90
print (df2[~df2['Name'].isin(df1['Name'])])
Name Add
2 D pt