How to Find the Intersection Between Series in Pandas


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

set(series1) & set(series2)

Recall that the intersection 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.

Additional Resources

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

How to Convert Pandas Series to DataFrame
How to Convert Pandas Series to NumPy Array
How to Merge Two or More Series in Pandas

Leave a Reply

Your email address will not be published.