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.

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


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.



My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.