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

Python Pandas - ส่งคืนอ็อบเจ็กต์ dtype ของ data พื้นฐาน


ในการส่งคืนวัตถุ dtype ของข้อมูลพื้นฐาน ให้ใช้ index.dtype ทรัพย์สินในหมีแพนด้า

ขั้นแรก นำเข้าไลบรารีที่จำเป็น -

import pandas as pd

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

index = pd.Index(['Car','Bike', 'Shop','Car','Airplace', 'Truck'])

แสดงดัชนี -

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

ส่งคืน dtype ของข้อมูล -

print("\nThe dtype object...\n",index.dtype)

ตัวอย่าง

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

import pandas as pd

# Creating the index
index = pd.Index(['Car','Bike', 'Shop','Car','Airplace', 'Truck'])

# Display the index
print("Pandas Index...\n",index)

# Return an array representing the data in the Index
print("\nArray...\n",index.values)

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

# Return a tuple of the shape of the underlying data
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,)