Change language

Python | Pandas Series.searchsorted ()

| |

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

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

# pandas module import

import pandas as pd 

 
# numpy module import

import numpy as np 

 
# create a list

list = [ 0  , 2 , 3 , 7 , 12 , 12 , 15 , 24 ]

 
# create series

series = pd.Series ( list )

 
# values ​​to insert

val = [ 1 , 7 , 14 ]

 
# calling the .searchsorted () method

result = series.searchsorted (value = val)

 
# display
result

Exit :

 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.

# pandas module import

import pandas as pd 

 
# numpy module import

import numpy as np 

 
# creating a list

data = [ ’apple’ , ’ banana’ , ’ mango’ , ’pineapple’  , ’pizza’ ]

  
# create series

series = pd.Series (data)

 
# values ​​to insert

val = [ ’grapes’ , ’ watermelon’ ]

 
# method call .searchsorted ()

result = series .searchsorted (value = val)

  
# display
result

Exit :

 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.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method