Open In App

Python | Pandas Series.searchsorted()

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Pandas 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)

Parameters:
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, searchsorted() method is called on a sorted series and 3-values are passed as parameter.




# importing pandas module 
import pandas as pd 
    
# importing numpy module 
import numpy as np 
    
# creating list
list =[0, 2, 3, 7, 12, 12, 15, 24]
  
# creating series
series = pd.Series(list)
  
# values to be inserted
val =[1, 7, 14]
  
# calling .searchsorted() method
result = series.searchsorted(value = val)
  
# display
result


Output:

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.

 

Example #2: 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 searchsorted() method.




# importing pandas module 
import pandas as pd 
    
# importing numpy module 
import numpy as np 
    
# creating list
data =['apple', 'banana', 'mango', 'pineapple', 'pizza']
  
# creating series
series = pd.Series(data)
  
# values to be inserted
val =['grapes', 'watermelon']
  
# calling .searchsorted() method
result = series.searchsorted(value = val)
  
# display
result


Output:

array([2, 5])

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.



Last Updated : 19 Dec, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads