Table of Contents
Finding the intersection between series in Pandas refers to identifying the common elements between two or more series objects in the Pandas library. This can be achieved by using the built-in “intersection” function, which compares the values of the series and returns a new series object containing only the elements that are present in all of the original series. This process can be useful for data analysis and manipulation, as it allows for the identification and extraction of shared data points between different series.
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 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:
Cite this article
stats writer (2024). How can I find the intersection between series in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-find-the-intersection-between-series-in-pandas/
stats writer. "How can I find the intersection between series in Pandas?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-find-the-intersection-between-series-in-pandas/.
stats writer. "How can I find the intersection between series in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-find-the-intersection-between-series-in-pandas/.
stats writer (2024) 'How can I find the intersection between series in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-find-the-intersection-between-series-in-pandas/.
[1] stats writer, "How can I find the intersection between series in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can I find the intersection between series in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
