Python

Pandas Series Intersection

This article will cover everything you need to know about finding the intersection of two series in Pandas. Using the default intersection function in Pandas, you can simply determine the intersection() of two series, sets, or even data frames. This article will provide all of the information you require about the Pandas Series Intersection. We’ve also included some examples for you to follow along.

Intersection() Method in Python

The intersection() method is one of many built-in functions provided by Python. It is one of the most popular methods for doing Python set intersection and is widely used by programmers.

The intersection() function in Python has the following syntax:

# set_one.intersection(set_two, set_three, set_four….)

The intersection() function in Python returns a new set containing a common element to all sets. The intersection of two sets is the largest set, including the values common to both sets. The intersection of two sets is a set that contains all of the elements that both sets share.

Now let’s cover some practical examples which will help you understand the previous theoretical explanation.

Example 1

The code below demonstrates how to find the intersection of two Pandas series. We’ll use the “&” operator to get an intersection in this example. The set intersection operator (&) lets the users connect two or even more than two sets together.

Look at the following code. The Pandas module has been imported here. Pandas is a Python open-source library. It delivers high-performance data structures and data analysis tools that are ready to use. Pandas is a Python module that operates on top of NumPy and is widely used for data science and analytics.

After importing the module, we have created two series with the names series_one and series_two. The series_one contains 5, 6, 6, 8, 12, 10, 12 values and 5, 6, 7, 9, 12, 14, 16 values are included in the series_two. The variable “result” is created afterward, in which the intersection completed these two series. Finally, the result of the intersection is displayed:

import pandas
series_one = pandas.Series([5, 6, 6, 8, 12, 10, 12])
series_two = pandas.Series([5, 6, 7, 9, 12, 14, 16])
result = set(series_one) & set(series_two)
print(result)

The outcome is a set with the numbers: 12, 5, and 6. The only three values that appear in both series are these three numbers:

It would be helpful to note that the same syntax can also work with Pandas series that contain strings. Here, you can see that series_one contains ‘B’, ‘C’, ‘D’, ‘E’, ‘F’ and ‘A’, ‘B’, ‘C’, ‘C’, ‘G’ is included in series_two. The intersection method is used again, and the following result is displayed:

import pandas
series_one = pandas.Series(['B', 'C', 'D', 'E', 'F'])
series_two = pandas.Series(['A', 'B', 'C', 'C', 'G'])
print(set(series_one) & set(series_two))

‘C’ and ‘B’ are the only strings that appear in both the first and second series. See the attached image for reference:

Example 2

The following code demonstrates how to find the intersection of the three Pandas Series. After importing the module, we created three series with the names series_one, series_two, and series_three. The series_one contains 3, 4, 4, 6, 8, 12, 14 values and series_two contain 5, 12, 4, 7, 9, 10, 16.

Similarly, 4, 6, 5, 8, 12, 20, 24 are included in series_three. After that, we used the & operator to find the intersection between these three series and saved the result in the variable “outcome”. Finally, the result is displayed below:

import pandas
series_one = pandas.Series([3, 4, 4, 6, 8, 12, 14])
series_two = pandas.Series([5, 12, 4, 7, 9, 10, 16])
series_three = pandas.Series([4, 6, 5, 8, 12, 20, 24])
outcome = set(series_one) & set(series_two) & set(series_three)
print(outcome)

The outcome is a set with the numbers 4 and 12 in it. Only these values are found in all three Series.

Example 3

In this example, we will explain the process of the set intersection() method. In this example, we are not going to use the Python set intersection operator (&) instead the complete name is used.

The three sets are created with the names: set_one, set_two, and set_three. The first set (set_one) contains 3, 5, 6, 6 values, the second set (set_two) contains 1, 5, 6, 4 values and our third set (set_three) contains 5, 8, 5 values. After that, we executed the intersection function on set_one with set_two and set_one with set_three. The resultant values are displayed afterward.

set_one = {3, 5, 6, 6}
set_two = {1, 5, 6, 4}
set_three = {5, 8, 5}
print("set_one intersection set_two : ",
    set_one.intersection(set_two))
print("set_one intersection set_two intersection set_three:",
    set_one.intersection(set_two, set_three))

Here, the intersection of the first set with the second set is displayed in the first line of the output. It gives us 5, 6 as a result which means that the values 5 and 6 are present in both sets. Likewise, the second line shows the intersection between the first set and the third set.

Example 4

In this example, we are using the Python set intersection operator (&) once again with the usage of the intersection operator. Here, three sets are created (set_one, set_two, set_three) containing different values. The intersection is done between these sets the way we have one in the previous examples.

set_one = {5, 6, 2, 1}
set_two = {2, 2, 5, 9}
set_three = {6, 2, 15}
print(set_one & set_two)
print(set_one & set_three)
print(set_one & set_two & set_three)

The first line of the output screen shows similar values in the first and the second set. The second line shows similar values in the first set and the thirst set. Lastly, the same values in all three sets are displayed.

Example 5

This example is about an empty Python set intersection. Here, we are going to execute the same steps as we have done in the previous examples but on empty sets. See the following sample code:

set_one = {}
set_two = {}
print("set_one intersection set_two : ", set(set_one).intersection(set(set_two)))

The following output shows no values:

Conclusion

In this article, we discussed the idea of the Pandas series intersection in-depth with examples for your convenience. The intersection() function in Python joins two Index objects together. This method creates a new Index containing elements that are common to the index. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content