ในการดูออบเจกต์ dtype ของข้อมูลในดัชนี (Index) ให้ใช้คุณสมบัติ index.dtype ใน Pandas
ขั้นตอนการทำงาน
1. นำเข้าไลบรารีที่จำเป็น
import pandas as pd
2. สร้างดัชนี
index = pd.Index(['Car', 'Bike', 'Shop', 'Car', 'Airplace', 'Truck'])
3. แสดงดัชนี
print("Pandas Index...\n", index)
4. ดู dtype ของข้อมูล
print("\nThe dtype object...\n", index.dtype)
ตัวอย่างโค้ดสมบูรณ์
import pandas as pd
# สร้างดัชนี
index = pd.Index(['Car', 'Bike', 'Shop', 'Car', 'Airplace', 'Truck'])
# แสดงดัชนี
print("Pandas Index...\n", index)
# แสดงอาร์เรย์ของข้อมูล
print("\nArray...\n", index.values)
# ดู dtype ของข้อมูล
print("\nThe dtype object...\n", index.dtype)
# แสดงรูปร่างของข้อมูล
print("\nA tuple of the shape of underlying data...\n", index.shape)
ผลลัพธ์
Pandas Index... Index(['Car', 'Bike', 'Shop', 'Car', 'Airplace', 'Truck'], dtype='object')
Array... ['Car' 'Bike' 'Shop' 'Car' 'Airplace' 'Truck']
The dtype object... object
A tuple of the shape of underlying data... (6,)
คำอธิบาย
index.dtype คืนค่าชนิดข้อมูล (dtype) ของค่าในดัชนี สำหรับข้อมูลสตริง จะเป็น 'object' สำหรับตัวเลขจำนวนเต็มจะเป็น 'int64' ทศนิยมจะเป็น 'float64' เป็นต้น