Python | Pandas Series.searchsorted()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
searchsorted() is a method for sorted series. It allows user to pass values as parameter which are to be inserted into the series and returns array of positions where values can be inserted so that the order of 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
In this example,
searchsorted() method is called on a sorted series and 3-values are passed as parameter.
array([1, 3, 6])
As shown in output, index of each value was returned. Since 7 already exists in series, index position 6 was returned for it because of the default side parameter which is ‘left’. Hence it returns left side index in case of equal values.
Searchsorted() on series of string.
In this example, a sorted series of some fruits name is made out of a python list using Pandas Series method. After that a list of two strings is passed as value parameter of
As shown in output, the index position is returned for every value in passed list so that the order of series will be preserved if the values are put at that index.