เพื่อคำนวณตำแหน่งสไลด์ (slice locations) สำหรับฉลากอินพุต ให้ใช้เมธอด index.slice_locs() ก่อนอื่น ให้นำเข้าไลบรารีที่จำเป็น:
import pandas as pd
สร้างออบเจกต์ Pandas Index:
index = pd.Index(list('pqrstuvwxyz'))
แสดง Pandas Index:
print("Pandas Index...\n", index)
ดึงตำแหน่งสไลด์ start คือฉลากเริ่มต้น end คือฉลากสิ้นสุด:
print("\nThe slice locations with start and stop...\n", index.slice_locs(start='q', end='v'))
ตัวอย่างโค้ดสมบูรณ์
import pandas as pd
# สร้าง Pandas index object
index = pd.Index(list('pqrstuvwxyz'))
# แสดง Pandas index
print("Pandas Index...\n", index)
# คืนค่าจำนวนสมาชิกใน Index
print("\nNumber of elements in the index...\n", index.size)
# ดึงตำแหน่งสไลด์
# start คือฉลากเริ่มต้น
# end คือฉลากสิ้นสุด
print("\nThe slice locations with start and stop...\n", index.slice_locs(start='q', end='v'))
ผลลัพธ์
โค้ดจะให้ผลลัพธ์ดังนี้:
Pandas Index... Index(['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], dtype='object') Number of elements in the index... 11 The slice locations with start and stop... (1, 7)