Python Pandas - Get integer location for requested label


To get the integer location for requested label in Pandas, use the index.get_loc() method. At first, import the required libraries −

import pandas as pd

Create Pandas index object −

index = pd.Index(list('pqrstuvwxyz'))

Display the Pandas index −

print("Pandas Index...\n",index)

Get integer location from the given index −

print("\nDisplay integer location from given index...\n",index.get_loc('w'))
print("\nDisplay integer location from given index...\n",index.get_loc('z'))

Example

Following is the code −

import pandas as pd

# create Pandas index object
index = pd.Index(list('pqrstuvwxyz'))

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

# get integer location from the given index
print("\nDisplay integer location from given index...\n",index.get_loc('w'))
print("\nDisplay integer location from given index...\n",index.get_loc('z'))

Output

This will produce the following output −

Pandas Index...
Index(['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], dtype='object')

Number of elements in the index...
11

Display integer location from given index...
7

Display integer location from given index...
10

Updated on: 14-Oct-2021

315 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements