ในการดูการใช้หน่วยความจำของค่าในดัชนี (Index) ให้ใช้เมธอด index.memory_usage() ใน Pandas
ขั้นตอนการทำงาน
1. นำเข้าไลบรารีที่จำเป็น
import pandas as pd
2. สร้างดัชนี
index = pd.Index([15, 25, 35, 45, 55])
3. แสดงดัชนี
print("Pandas Index...\n", index)
4. ดูการใช้หน่วยความจำ
print("\nThe memory usage...\n", index.memory_usage())
ตัวอย่างโค้ดสมบูรณ์
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)
# ดูการใช้หน่วยความจำ
print("\nThe memory usage...\n", index.memory_usage())
ผลลัพธ์
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
The memory usage... 40
คำอธิบาย
memory_usage() คืนค่าจำนวนไบต์ที่ใช้จัดเก็บข้อมูลในดัชนี รวมถึงโครงสร้างข้อมูลเบื้องต้น สำหรับ Int64Index 5 ค่า ใช้ 40 ไบต์ (5 × 8 ไบต์)