Index.isin(self, values, level=None)
[source]
Return a boolean array where the index values are in values
.
Compute boolean array of whether each index value is found in the passed set of values. The length of the returned boolean array matches the length of the index.
Parameters: |
|
---|---|
Returns: |
|
See also
Series.isin
DataFrame.isin
In the case of MultiIndex
you must either specify values
as a list-like object containing tuples that are the same length as the number of levels, or specify level
. Otherwise it will raise a ValueError
.
If level
is specified:
>>> idx = pd.Index([1,2,3]) >>> idx Int64Index([1, 2, 3], dtype='int64')
Check whether each index value in a list of values. >>> idx.isin([1, 4]) array([ True, False, False])
>>> midx = pd.MultiIndex.from_arrays([[1,2,3], ... ['red', 'blue', 'green']], ... names=('number', 'color')) >>> midx MultiIndex(levels=[[1, 2, 3], ['blue', 'green', 'red']], codes=[[0, 1, 2], [2, 0, 1]], names=['number', 'color'])
Check whether the strings in the ‘color’ level of the MultiIndex are in a list of colors.
>>> midx.isin(['red', 'orange', 'yellow'], level='color') array([ True, False, False])
To check across the levels of a MultiIndex, pass a list of tuples:
>>> midx.isin([(1, 'red'), (3, 'red')]) array([ True, False, False])
For a DatetimeIndex, string values in values
are converted to Timestamps.
>>> dates = ['2000-03-11', '2000-03-12', '2000-03-13'] >>> dti = pd.to_datetime(dates) >>> dti DatetimeIndex(['2000-03-11', '2000-03-12', '2000-03-13'], dtype='datetime64[ns]', freq=None)
>>> dti.isin(['2000-03-11']) array([ True, False, False])
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.25.0/reference/api/pandas.Index.isin.html