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

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically