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.
Index.searchsorted() function find indices where elements should be inserted to maintain order. The function find the indices into a sorted IndexOpsMixin self such that, if the corresponding elements in value were inserted before the indices, the order of self would be preserved.
Syntax: Index.searchsorted(value, side=’left’, sorter=None)
value : Values to insert into self.
side : If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index. If there is no suitable index, return either 0 or N (where N is the length of self).
sorter : Optional array of integer indices that sort self into ascending order. They are typically the result of np.argsort.
Returns : [indices : array of ints] Array of insertion points with the same shape as value.
Example #1: Use
Index.searchsorted() function to find the correct position to insert an element such that the Index remains sorted.
Let’s find the position of for insertion if the element to be inserted is 10
As we can see in the output, the function has returned 4 indicating that the correct position to insert 10 in the index is 4 if the order is to be maintained.
Example #2: Use
Index.searchsorted() function to find the correct position of insertion for more than one elements in the Index. The insertion should be done such that the order is maintained.
Let’s find the position of for insertion if the element to be inserted is 7 and 29
As we can see in the output, the function has returned 2 and 6 indicating that the correct position to insert 7 and 29 in the index is 2nd and 6th position if the order is to be maintained.
- Python | pandas.to_markdown() in Pandas
- Python | pandas.map()
- Python | Pandas Series.str.pad()
- Python | Pandas Series.between()
- Python | Pandas dataframe.sem()
- Python | Pandas dataframe.std()
- Python | Pandas Series.var
- Python | Pandas Series.where
- Python | Pandas Series.div()
- Python | Pandas Series.dt.day
- Python | Pandas TimedeltaIndex.contains
- Python | Pandas Series.xs
- Python | Pandas Panel.sum()
- Python | Pandas Period.second
- Python | Pandas Series.loc
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.