เพื่อหาตำแหน่งจำนวนเต็มของป้ายกำกับที่ร้องขอใน Pandas ให้ใช้เมธอด index.get_loc()
เริ่มต้นด้วยการนำเข้าไลบรารีที่จำเป็น:
import pandas as pd
สร้างอ็อบเจกต์ Pandas Index:
index = pd.Index(list('pqrstuvwxyz'))
แสดง Pandas Index:
print("Pandas Index...\n", index)
หาตำแหน่งจำนวนเต็มจาก 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'))
ตัวอย่างโค้ดเต็ม
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'))
เอาต์พุต
จะได้ผลลัพธ์ดังนี้:
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