ในการคำนวณขอบเขตสไลซ์ด้านซ้ายที่สอดคล้องกับป้ายกำกับที่กำหนด ให้ใช้ index.get_slice_bound() . ตั้งค่า ด้าน พารามิเตอร์เป็น ซ้าย .
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
การสร้างดัชนีนุ่น -
index = pd.Index([10, 20, 30, 40, 50, 60, 70])
แสดงดัชนีหมีแพนด้า −
print("Pandas Index...\n",index) รับขอบด้านซ้าย ส่งคืนขอบด้านซ้ายของป้ายกำกับที่กำหนดหากตั้งค่าพารามิเตอร์ "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