Python Pandas:返回底层数据类型的 dtype 对象


若要返回底层数据的 dtype 对象,请在 Pandas 中使用 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)

示例

以下是代码-

Open Compiler
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,)

更新于: 2021 年 10 月 13 日

381 次浏览

开启您的 事业

完成课程以获得认证

开始
广告