w3resource

Pandas Series: isin() function

Find values contained in Pandas series

The isin() function is used to check whether values are contained in Series.

Return a boolean Series showing whether each element in the Series matches an element in the passed sequence of values exactly.

Syntax:

Series.isin(self, values)
Pandas Series isin image

Parameters:

Name Description Type/Default Value Required / Optional
values The sequence of values to test. Passing in a single string will raise a TypeError. Instead, turn a single string into a list of one element.
New in version 0.18.1: Support for values as a set.
set or list-like Required

Returns: Series
Series of booleans indicating if each element is in values.

Raises: TypeError
If values is a string

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['dog', 'cow', 'dog', 'cat', 'dog',
               'lion'], name='animal')
s.isin(['cow', 'dog'])

Output:

0     True
1     True
2     True
3    False
4     True
5    False
Name: animal, dtype: bool

Example - Passing a single string as s.isin('dog') will raise an error. Use a list of one element instead:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['dog', 'cow', 'dog', 'cat', 'dog',
               'lion'], name='animal')
s.isin(['dog'])

Output:

0     True
1    False
2     True
3    False
4     True
5    False
Name: animal, dtype: bool

Previous: Row label of the minimum value in Pandas series
Next: Subsetting final periods of time in Pandas series



Follow us on Facebook and Twitter for latest update.