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

Python - ตรวจสอบว่าดัชนี Pandas เป็นของวัตถุ dtype . หรือไม่


หากต้องการตรวจสอบว่าดัชนี Pandas เป็นวัตถุ dtype หรือไม่ ให้ใช้ index.is_object() กระบวนการ. ขั้นแรก นำเข้าไลบรารีที่จำเป็น -

import pandas as pd

การสร้างดัชนีนุ่น -

index = pd.Index(["Electronics", 6, 10.5, "Accessories", 25.6, 30])

แสดงดัชนีหมีแพนด้า −

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

ตรวจสอบว่าค่าดัชนีมีวัตถุ dtype หรือไม่ -

print("\nIs the Index of object dtype?\n", index.is_object())

ตัวอย่าง

ต่อไปนี้เป็นรหัส -

import pandas as pd

# Creating Pandas index
index = pd.Index(["Electronics", 6, 10.5, "Accessories", 25.6, 30])

# 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)

# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)

# check whether index values has object dtype
print("\nIs the Index of object dtype?\n", index.is_object())

ผลลัพธ์

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

Pandas Index...
Index(['Electronics', 6, 10.5, 'Accessories', 25.6, 30], dtype='object')

Number of elements in the index...
6

The dtype object...
object

Is the Index of object dtype?
True