
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Compute indexer and find the next index value if no exact match
To compute indexer and find the next index value if no exact match, use the index.get_indexer() method. Also set the method parameter to bfill.
At first, import the required libraries −
import pandas as pd
Creating Pandas index −
index = pd.Index([10, 20, 30, 40, 50, 60, 70])
Display the Pandas index −
print("Pandas Index...\n",index)
Compute indexer and mask using the "get_indexer". Find the next index value if no exact match using the "method" parameter. The value is set "bfill" −
print("\nGet the indexes...\n",index.get_indexer([30, 25, 58, 50, 55], method="bfill"))
Example
Following is the code −
import pandas as pd # Creating Pandas index index = pd.Index([10, 20, 30, 40, 50, 60, 70]) # Display the Pandas index print("Pandas Index...\n",index) # Return the number of elements in the Index print("\nNumber of elements in the index...\n",index.size) # Compute indexer and mask using the "get_indexer" # Find the next index value if no exact match using the "method" parameter. # The value is set "bfill" print("\nGet the indexes...\n",index.get_indexer([30, 25, 58, 50, 55], method="bfill"))
Output
This will produce the following output −
Pandas Index... Int64Index([10, 20, 30, 40, 50, 60, 70], dtype='int64') Number of elements in the index... 7 Get the indexes... [2 2 5 4 5]
- Related Articles
- Python Pandas - Compute indexer and find the previous index value if no exact match
- Python Pandas - Compute indexer and find the nearest index value if no exact match
- Python Pandas - Get integer location for requested label and find the previous index value if no exact match
- Python Pandas - Get integer location for requested label and find the nearest index value if no exact match
- Python Pandas - Compute indexer and mask for new index given the current index
- Python Pandas - Compute indexer and mask for new index even for non-uniquely valued objects
- Python Pandas - Compute the slice indexer for input labels
- Python Pandas - Compute the symmetric difference of two Index objects
- Python Pandas - Compute the symmetric difference of two Index objects and unsort the result
- Python Pandas - Check if the Pandas Index holds Interval objects
- Python - Return the minimum value of the Pandas Index
- Python - Return the maximum value of the Pandas Index
- Python Pandas - Check if the index has NaNs
- Python - Check if the Pandas Index holds categorical data
- Python Pandas - Check if the index has unique values

Advertisements