Pandas Series.isin()
function is used to check whether values are contained in the given Series object or not. It returns a Series of booleans indicating True when present, and False when not. In this article, I will explain the syntax of Series.isin() function, its parameters, and how to check whether values are contained in a given Series object with examples.
Key Points –
Series.isin()
is used to check whether each element in a Series is contained in a specified list, set, or another iterable.- The function returns a Series of Boolean values, where
True
indicates that the element is present in the given iterable andFalse
indicates it is not. - The length of the input iterable does not need to match the length of the original Series.
Series.isin()
is often used for filtering or subsetting data, based on whether elements are part of a specified collection.- The function does not modify the original Series; instead, it returns a new Boolean Series.
Quick Examples of Series.isin() Function
If you are in a hurry, below are some quick examples of the Pandas Series.isin() function.
# Quick examples of Series.isin() function
# Example 1: Use series.isin() function
# To Single Value
ser2 = ser.isin([20])
# Example 2: Use series.isin() function
# To multiple values
ser2 = ser.isin([20, 35])
# Example 3: Use ~ operator
ser2 = ~ser.isin([20, 58])
Syntax of Series.isin()
Following is the syntax of Series.isin() function.
# Syntax of Series.isin()
Series.isin(values)
Values parameter accepts set or list-like.
Use Series.isin() Function to Single Value
You can use Series.isin()
function to check single value is contained in the given Series object or not. For example, isin()
function returns a Series of booleans indicating True when present, and False when not.
Now, let’s create pandas series using a list of values and use the max() function.
import pandas as pd
import numpy as np
# Create a Series
ser = pd.Series([15, 20, 58, 10, 35, 49, 20, 15])
print(ser)
Yields below output.
# Output:
0 15
1 20
2 58
3 10
4 35
5 49
6 20
7 15
dtype: int64
Let’s use the isin() now.
# Use series.isin() function
# To Single Value
ser2 = ser.isin([20])
print(ser2)
Yields below output.
# Output:
0 False
1 True
2 False
3 False
4 False
5 False
6 True
7 False
dtype: bool
Use Series.isin() Function to Multiple Values
Similarly, you can also use Series.isin()
function to check whether multiple values are contained in the given Series object or not. For example, Use isin()
function has returned an object containing boolean values. All values have been mapped to True if it is present in the list else False.
# Use series.isin() function
# To multiple values
ser2 = ser.isin([20, 35])
print(ser2)
Yields below output. Observe that the values at index 1
, 4
, and 6
are matched to the given sequence of values, while the remaining are not.
# Output:
0 False
1 True
2 False
3 False
4 True
5 False
6 True
7 False
dtype: bool
Use ~ Operator
Let’s create a Series and then use the ~
operator to filter out certain values.
# Use ~ operator
ser2 = ~ser.isin([20, 58])
print(ser2)
Yields below output.
# Output:
0 True
1 False
2 False
3 True
4 True
5 True
6 False
7 True
dtype: bool
Complete Example of Series.isin() Function
import pandas as pd
import numpy as np
# Create a Series
ser = pd.Series([15, 20, 58, 10, 35, 49, 20, 15])
print(ser)
# Use series.isin() function
# To Single Value
ser2 = ser.isin([20])
print(ser2)
# Use series.isin() function
# To multiple values
ser2 = ser.isin([20, 35])
print(ser2)
# Use ~ operator
ser2 = ~ser.isin([20, 58])
print(ser2)
FAQ on Pandas Series.isin() Function
The Series.isin()
function checks whether each element in a Pandas Series is contained in a specified set of values. It returns a Boolean Series, where each element is True
if it is in the given list or set, and False
otherwise.
To use the Series.isin()
function, you need to pass a list, set, or any other iterable of values to check if each element in the Series is present in that collection. It returns a Boolean Series with True
or False
for each element based on whether it is found in the provided collection.
You can use Series.isin()
with any data type that is stored in the Series, including integers, floats, strings, and even datetime objects. The values you check against must be in a list-like structure (list, set, tuple, or another Series).
To filter a Series based on values, you can use isin()
inside a boolean indexing operation.
Series.isin()
treats NaN
as a distinct value. If NaN
is part of the values you are checking against.
You can use the ~
operator to negate the result, effectively checking for values that are not in the specified set.
You can use Series.isin()
with strings. The function works the same way as with other data types. You simply pass a list, set, or any iterable containing the string values you want to check against, and it returns a Boolean Series indicating whether each string in the Series is present in the provided list.
Conclusion
In this article, I have explained the pandas Series isin() function that returns a Series of booleans indicating True when present, and False when not for each value of the Series.
Happy Learning !!
Related Articles
- Pandas Series.max() Function
- Pandas Series.mean() Function
- Pandas Series.fillna() Function
- How to Reshape Pandas Series
- How to Rename a Pandas Series
- Pandas Get Floor or Ceil of Series
- How to Convert List to Pandas Series
- Pandas Remove Elements From Series
- Create a Set From a Series in Pandas
- Add Column Name to Pandas Series
- Check Values of Pandas Series is Unique
- Pretty Print Pandas DataFrame or Series
- How To Get Value From Pandas Series?