• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:7 mins read
You are currently viewing Pandas Series.isin() Function

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.

1. Quick Examples of Series.isin() Function

If you are in a hurry, below are some quick examples of the Pandas Series.isin() function.


# Below are quick examples.

# 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])

2. 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.

3. 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

4. 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

6. Use ~ Operator


# 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

7. 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)

8. 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

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium