Wednesday, February 4, 2026
HomeLanguagesIntersection of two dataframe in Pandas – Python

Intersection of two dataframe in Pandas – Python

Intersection of Two data frames in Pandas can be easily calculated by using the pre-defined function merge(). This function takes both the data frames as argument and returns the intersection between them.

Syntax:

pd.merge(df1, df2, how)

Example 1:




import pandas as pd
  
# Creating Data frames
df1 = {'A': [1, 2, 3, 4],
         'B': ['abc', 'def', 'efg', 'ghi']} 
df2 = {'A': [1, 2, 3, 4 ],
         'B': ['Geeks', 'For', 'efg', 'ghi'],
         'C':['Nikhil', 'Rishabh', 'Rahul', 'Shubham']} 
           
d1 = pd.DataFrame(df1)
d2 = pd.DataFrame(df2) 
  
# Calling merge() function
int_df = pd.merge(d1, d2, how ='inner', on =['A', 'B'])
print(int_df)


Output:

   A    B        C
0  3  efg    Rahul
1  4  ghi  Shubham

Example 2:




import pandas as pd
  
# Creating Data frames
df1 = {'A': [1, 2, 3, 4],
         'B': ['Geeks', 'For', 'efg', 'ghi']} 
df2 = {'A': [1, 2, 3, 4 ],
         'B': ['Geeks', 'For', 'abc', 'cde'],
         'C':['Nikhil', 'Rishabh', 'Rahul', 'Shubham']} 
           
d1 = pd.DataFrame(df1)
d2 = pd.DataFrame(df2) 
  
# Calling merge() function
int_df = pd.merge(d1, d2, how='inner', on=['A', 'B'])
print(int_df)


Output:

   A      B        C
0  1  Geeks   Nikhil
1  2    For  Rishabh
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32479 POSTS0 COMMENTS
Milvus
125 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11980 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6936 POSTS0 COMMENTS
Umr Jansen
6919 POSTS0 COMMENTS