เพื่อคำนวณขอบเขตซ้าย (left slice bound) ที่ตรงกับป้ายกำกับที่กำหนด ให้ใช้เมธอด index.get_slice_bound() ตั้งค่าพารามิเตอร์ side เป็น left
เริ่มต้นด้วยการนำเข้าไลบรารีที่จำเป็น:
import pandas as pd
สร้าง Pandas Index:
index = pd.Index([10, 20, 30, 40, 50, 60, 70])
แสดง Pandas Index:
print("Pandas Index...\n", index)
ดึงขอบเขตซ้าย (left slice bound) คืนค่าขอบเขตซ้ายของป้ายกำกับที่กำหนด หากพารามิเตอร์ "side" ตั้งเป็น "left":
print("\nGet the left slice bound...\n", index.get_slice_bound(30, side='left', kind='getitem'))
ตัวอย่างโค้ดเต็ม
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)
# Get the left slice bound
# Returns the left slice bound of given label if "side" parameter is set to "left"
print("\nGet the left slice bound...\n", index.get_slice_bound(30, side='left', kind='getitem'))
เอาต์พุต
จะได้ผลลัพธ์ดังนี้:
Pandas Index... Int64Index([10, 20, 30, 40, 50, 60, 70], dtype='int64') Number of elements in the index... 7 Get the left slice bound... 2