ในการหาจำนวนองค์ประกอบในดัชนี (Index) ให้ใช้คุณสมบัติ index.size ใน Pandas
ขั้นตอนการทำงาน
1. นำเข้าไลบรารีที่จำเป็น
import pandas as pd
2. สร้างดัชนี
index = pd.Index([15, 25, 35, 45, 55])
3. แสดงดัชนี
print("Pandas Index...\n", index)
4. หาจำนวนองค์ประกอบ
print("\nNumber of elements in the index...\n", index.size)
ตัวอย่างโค้ดสมบูรณ์
import pandas as pd
# สร้างดัชนี
index = pd.Index([15, 25, 35, 45, 55])
# แสดงดัชนี
print("Pandas Index...\n", index)
# แสดงจำนวนองค์ประกอบ
print("\nNumber of elements in the index...\n", index.size)
# แสดงรูปร่างของข้อมูล
print("\nA tuple of the shape of underlying data...\n", index.shape)
# แสดงไบต์ของข้อมูล
print("\nReturn the bytes...\n", index.nbytes)
# แสดงมิติของข้อมูล
print("\nReturn the dimensions...\n", index.ndim)
ผลลัพธ์
Pandas Index... Int64Index([15, 25, 35, 45, 55], dtype='int64')
Number of elements in the index... 5
A tuple of the shape of underlying data... (5,)
Return the bytes... 40
Return the dimensions... 1
คำอธิบาย
index.size คืนค่าจำนวนองค์ประกอบทั้งหมดในดัชนี เทียบเท่ากับ len(index)