How to find the intersection between series in Pandas?

In order to find the intersection between series in Pandas, you can use the intersection() function. This function takes two series as its arguments and returns a new series that contains only the values that appear in both of the given series. This is useful for analyzing data sets and combining information from multiple sources.


You can use the following basic syntax to find the intersection between two Series in pandas:

set(series1) & set(series2)

Recall that the of two sets is simply the set of values that are in both sets.

The following examples show how to calculate the intersection between pandas Series in practice.

Example 1: Calculate Intersection Between Two Pandas Series

The following code shows how to calculate the intersection between two pandas Series:

import pandas as pd

#create two Series
series1 = pd.Series([4, 5, 5, 7, 10, 11, 13])
series2 = pd.Series([4, 5, 6, 8, 10, 12, 15])

#find intersection between the two series
set(series1) & set(series2)

{4, 5, 10}

The result is a set that contains the values 4, 5, and 10.

These are the only three values that are in both the first and second Series.

Also note that this syntax works with pandas Series that contain strings:

import pandas as pd

#create two Series
series1 = pd.Series(['A', 'B', 'C', 'D', 'E'])
series2 = pd.Series(['A', 'B', 'B', 'B', 'F'])

#find intersection between the two series
set(series1) & set(series2)

{'A', 'B'}

The only strings that are in both the first and second Series are A and B.

Example 2: Calculate Intersection Between Three Pandas Series

The following code shows how to calculate the intersection between three pandas Series:

import pandas as pd

#create three Series
series1 = pd.Series([4, 5, 5, 7, 10, 11, 13])
series2 = pd.Series([4, 5, 6, 8, 10, 12, 15])
series3 = pd.Series([3, 5, 6, 8, 10, 18, 21])

#find intersection between the three series
set(series1) & set(series2) & set(series3)

{5, 10}

The result is a set that contains the values 5 and 10.

These are the only values that are in all three Series.

The following tutorials explain how to perform other common operations with Series in pandas:

x