Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python Pandas - หาจำนวนไบต์ของข้อมูลดัชนี (index.nbytes)

ในการหาจำนวนไบต์ของข้อมูลในดัชนี (Index) ให้ใช้คุณสมบัติ index.nbytes ใน Pandas

ขั้นตอนการทำงาน

1. นำเข้าไลบรารีที่จำเป็น

import pandas as pd

2. สร้างดัชนี

index = pd.Index([15, 25, 35, 45, 55])

3. แสดงดัชนี

print("Pandas Index...\n", index)

4. หาจำนวนไบต์

print("\nReturn the bytes...\n", index.nbytes)

ตัวอย่างโค้ดสมบูรณ์

import pandas as pd

# สร้างดัชนี
index = pd.Index([15, 25, 35, 45, 55])

# แสดงดัชนี
print("Pandas Index...\n", index)

# แสดงอาร์เรย์ของข้อมูล
print("\nArray...\n", index.values)

# แสดงรูปร่างของข้อมูล
print("\nA tuple of the shape of underlying data...\n", index.shape)

# หาจำนวนไบต์
print("\nReturn the bytes...\n", index.nbytes)

ผลลัพธ์

Pandas Index... Int64Index([15, 25, 35, 45, 55], dtype='int64')
Array... [15 25 35 45 55]
A tuple of the shape of underlying data... (5,)
Return the bytes... 40

คำอธิบาย

index.nbytes คืนค่าจำนวนไบต์ทั้งหมดที่ใช้จัดเก็บข้อมูลในดัชนี สำหรับ Int64Index 5 ค่า ใช้ 40 ไบต์ (5 × 8 ไบต์)