Series.searchsorted(self, value, side='left', sorter=None) [source]
Find indices where elements should be inserted to maintain order.
Find the indices into a sorted Series self such that, if the corresponding elements in value were inserted before the indices, the order of self would be preserved.
| Parameters: |
|
|---|---|
| Returns: |
|
See also
Binary search is used to find the required insertion points.
>>> x = pd.Series([1, 2, 3]) >>> x 0 1 1 2 2 3 dtype: int64
>>> x.searchsorted(4) 3
>>> x.searchsorted([0, 4]) array([0, 3])
>>> x.searchsorted([1, 3], side='left') array([0, 2])
>>> x.searchsorted([1, 3], side='right') array([1, 3])
>>> x = pd.Categorical(['apple', 'bread', 'bread',
'cheese', 'milk'], ordered=True)
[apple, bread, bread, cheese, milk]
Categories (4, object): [apple < bread < cheese < milk]
>>> x.searchsorted('bread')
1
>>> x.searchsorted(['bread'], side='right') array([3])
© 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.Series.searchsorted.html