While working with dataframes, many a times we have two dataframes and there is a need to find common elements between them. Such problems can be easily handled by merge function.
So this recipe is a short example on how to find common elements between 2 pandas dataframes. Let's get started.
import pandas as pd
Let's pause and look at these imports. Pandas is generally used for data manipulation and analysis.
df1= pd.DataFrame({'Student': ['Ram','Rohan','Shyam','Mohan'],
'Grade': ['A','C','B','Ex']})
df2 = pd.DataFrame({'Student': ['Ram','Shyam','Raunak'],
'Grade': ['A','B','F']})
Let us create a two simple dataset of Student and grades.
df3=pd.merge(df1,df2, how='inner')
Merge function in pandas library help us in performing all types of binary operation over dataframes. Here we simply merge both on all columns keep 'how' as 'inner' which simply means finding common elements.
print('df1\n',df1)
print('df2\n',df2)
print('df1 union df2\n',df3)
Simply use print function to print df1, df2 and our new dataframe df1 union df2
Once we run the above code snippet, we will see:
Scroll down to the ipython notebook below to see the output.