searchsorted() — this is a method for sorted rows. This allows the user to pass values as a parameter to be inserted into the series, and returns an array of positions where values can be inserted so that the order of the series is still preserved.
Syntax: Series.searchsorted (value, side = ’left’, sorter = None)
value : Values to be inserted into self (Caller series)
side: ’left’ or ’right’, returns first or last suitable position for value respectively
sorter: Array of indices which is of same size as series is passed. If sorter is None, caller series must be in ascending order, otherwise sorter should be array of indices that sorts it.
Return type: Array of indices
Example # 1:
In this example, the
searchsorted () method is called on a sorted series and 3 values are passed as a parameter.
array ([1, 3, 6])
As shown in the output, an index of each value was returned. Since 7 already exists sequentially, index position 6 was returned for it because of the default side parameter, which is "abandoned". Therefore, it returns the left side index in case of equal values.
Example # 2:
Searchsorted () in a sequence of lines.
In this example, a sorted series of fruit names consists of a list of pythons using the Pandas Series method. The two-line list is then passed as the
searchsorted () parameter of the
searchsorted () method.
array ([2, 5])
As shown in the output, the index position is returned for each value in the passed list, so that the order of the rows will be preserved if the values are placed in that index.