• Post author:
• Post category:Pandas

We can find the intersection between the two Pandas Series in different ways. Intersection means common elements of given Series. In this article, I will explain the intersection between two or more Series using the `set intersection operator (&)`.

It returns a set that contains common elements of both Series. In another, we can find out the intersection of two Series using the Pandas `index.intersection()` function.

Key Points –

• Use the `.intersection()` method to find common elements between two Pandas Series.
• Convert the Series to sets using the `set()` function to perform intersection operations.
• Utilize Pandas’ built-in methods and functions like `.intersection()` or `numpy.intersect1d()` for efficient intersection computation.
• Utilize boolean indexing to filter elements that exist in both Series.
• Ensure compatibility of data types and index alignment when finding intersections in Pandas Series.

## Quick Examples of Intersect Two Pandas Series

If you are in a hurry, below are some quick examples of how to Intersect two pandas Series.

``````
# Below are some quick examples

# Example 1: Find intersection between the two series
# Create two pandas Series
ser1 = pd.Series([1, 4, 5, 2, 5])
ser2 = pd.Series([5, 2, 3, 1, 6])
intersect = set(ser1) & set(ser2)

# Example 2: Find intersection between the two series
# Create pandas Series
ser1 = pd.Series(['Python', 'Pandas', 'Java', 'c'])
ser2 = pd.Series(['Spark', 'c', 'Java', 'PySpark'])
# Find intersection between the two series
intersect = set(ser1) & set(ser2)

# Example 3: Find intersection between three series
# Create pandas Series
ser1 = pd.Series(['Python', 'Pandas', 'Java', 'c'])
ser2 = pd.Series(['Spark', 'c', 'Java', 'PySpark'])
ser3 = pd.Series(['Spark', 'c', 'Hive', 'PySpark'])
intersect = set(ser1) & set(ser2) & set(ser3)

# Example 4: Find intersect of Series using index.intersection()
ser1 = pd.Index([1, 2, 3, 4])
ser2 = pd.Index([3, 4, 5, 6])
intersect = ser1.intersection(ser2)
``````

## Create Two Pandas Series

Pandas Series is a one-dimensional array that is capable of storing various data types (integer, string, float, python objects, etc.). We can easily convert the list, tuple, and dictionary into Series using the `pandas.Series()` function. The row labels of the Series are called the index. The Series can have only one column. A List, NumPy Array, and Dict can be turned into a pandas Series.

``````
import pandas as pd
# Create two pandas Series
ser1 = pd.Series([1, 4, 5, 2, 5])
print(ser1)

ser2 = pd.Series([5, 2, 3, 1, 6])
print(ser2)
``````

Yields below output.

``````
# Output:
0    1
1    4
2    5
3    2
4    5
dtype: int64

0    5
1    2
2    3
3    1
4    6
dtype: int64
``````

## Find Intersection Between Two Pandas Series

By using `&` operator you can find the intersection of the two Series. Using the `&` operator you can also get an intersection of more than two Series.

``````
# Find intersection between the two series
intersect = set(ser1) & set(ser2)
print(intersect)

# Output :
# {1, 2, 5}
``````

As you can see from the above code, it has returned a set of common values of two Series.

Now, let’s take two Series which contain elements as a string type, then apply the set intersection operator `'&'`. It will return common strings of given Series.

``````
# Create pandas Series
ser1 = pd.Series(['Python', 'Pandas', 'Java', 'c'])
print(ser1)
ser2 = pd.Series(['Spark', 'c', 'Java', 'PySpark'])
print(ser2)
intersect = set(ser1) & set(ser2)
print(intersect)

# Output:
# {'Java', 'c'}
``````

## Find Intersection Between Three Pandas Series

Take three Pandas Series and apply the set `'&'` operator, it will return a set of common strings of the given three Series.

``````
#create three Series
# Create pandas Series
ser1 = pd.Series(['Python', 'Pandas', 'Java', 'c'])
print(ser1)
ser2 = pd.Series(['Spark', 'c', 'Java', 'PySpark'])
print(ser2)
ser3 = pd.Series(['Spark', 'c', 'Hive', 'PySpark'])

# Find intersection between three series
intersect = set(ser1) & set(ser2) & set(ser3)
print(intersect)

# Output:
# {'c'}
``````

Similarly, to find the intersection between three Pandas Series, you can use the `isin()` method along with boolean indexing.

``````
# Find the intersection between the three Series
intersection = ser1[ser1.isin(ser2) & ser1.isin(ser3)]
print(intersection)

# Output:
# 3    c
dtype: object
``````

This output represents the elements that are common between all three Series, which is only the element ‘c’.

## Use Pandas index.intersection() Find Intersection of Series

Pandas `index.intersection()` is used to form the intersection of two Index objects. It returns a new index with elements common to the index.

``````
# Below is the syntax
Index.intersection(other, sort=False)
``````

Take to Series and apply `index.intersection()` method, it will return common new index which contains elements common in the specified indexes.

``````
# Find intersect of Series using index.intersection()
ser1 = pd.Index([1, 2, 3, 4])
ser2 = pd.Index([3, 4, 5, 6])
intersect = ser1.intersection(ser2)
print(intersect)

# Output:
# Int64Index([3, 4], dtype='int64')
``````

## Frequently Asked Questions on Find Intersection Between Two Series

How can I find the intersection between two Pandas Series?

To find the intersection between two Pandas Series using the `intersection()` method. This method returns a new Series containing only the elements that are common to both Series.

What happens if there are duplicate values in the Series?

If there are duplicate values in the Series, the `intersection()` method will return duplicates as well. It will keep the duplicates present in both Series.

Can I find intersections between more than two Series using vectorized operations?

You can find intersections between more than two Series using vectorized operations and logical operators like `&` for AND operations. However, the complexity might increase with the number of Series involved.

What is the difference between using isin() method and set operations to find the intersection?

The `isin()` method with boolean indexing directly filters the common elements from one Series based on whether they exist in the other Series. Set operations involve converting both Series into sets and then finding the intersection between the sets.

How can I find the intersection between two Pandas Series?

You can find the intersection between two Pandas Series using various methods, including.
Using the `isin()` method with boolean indexing.
Converting the Series to sets and using set operations like `&`.
Utilizing built-in Pandas functions like `.intersection()` or `numpy.intersect1d()`.

What does it mean to find the intersection between two Pandas Series?

Finding the intersection between two Pandas Series means identifying and extracting the elements that exist in both Series. In other words, it involves determining the common values shared between the two Series. This operation is similar to the mathematical concept of intersection, where you’re interested in the overlap or commonality between two sets of elements.

## Conclusion

In this article, I have explained how to use the set `'&'` operator and using this how we can intersect two or more Pandas Series with multiple examples as well as explained the `index.intersection()` function.

Happy Learning !!