w3resource

Pandas Series: searchsorted() function

Find indices where elements should be inserted to maintain order

The searchsorted() function is used to 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.

Syntax:

Series.searchsorted(self, value, side='left', sorter=None)
Pandas Series searchsorted image
Name Description Type/Default Value Required / Optional
value Values to insert into self. array_like Required
side If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index. If there is no suitable index, return either 0 or N (where N is the length of self).

{‘left’, ‘right’} Optional
sorter Optional array of integer indices that sort self into ascending order. They are typically the result of np.argsort. 1-D array_like Optional

Returns: int or array of int- A scalar or array of insertion points with the same shape as value. Changed in version 0.24.0: If value is a scalar, an int is now always returned. Previously, scalar inputs returned an 1-item array for Series and Categorical.

Notes:

Binary search is used to find the required insertion points.

Example:


Download the above Notebook from here.



Inviting useful, relevant, well-written and unique guest posts