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.
1. 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)
2. 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
3. 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'}
4. 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'}
5. 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')
6. 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 !!
Related Articles
- Pandas Get First Column of DataFrame as Series
- Convert Pandas Series of Lists to One Series
- Check Values of Pandas Series is Unique
- Convert Pandas Series to NumPy Array
- Convert Pandas DataFrame to Series
- Remove NaN From Pandas Series
- Create a Set From a Series in Pandas
- Pandas Series filter() Function