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