Python Pandas - 返回底层数据的 dtype 对象
pythonpandasserver side programmingprogramming更新于 2025/12/30 8:12:17
要返回底层数据的 dtype 对象,请使用 Pandas 中的 index.dtype 属性。
首先,导入所需的库 −
import pandas as pd
创建索引 −
index = pd.Index(['Car','Bike', 'Shop','Car','Airplace', 'Truck'])
显示索引−
print("Pandas Index...\n",index)
返回数据的 dtype −
print("\ndtype 对象...\n",index.dtype)
示例
以下是代码 −
import pandas as pd
# 创建索引
index = pd.Index(['Car','Bike', 'Shop','Car','Airplace','Truck'])
# 显示索引
print("Pandas Index...\n",index)
# 返回表示 Index 中数据的数组
print("\n数组...\n",index.values)
# 返回数据的 dtype
print("\ndtype 对象...\n",index.dtype)
# 返回底层数据形状的元组
print("\n底层数据形状的元组...\n",index.shape)
输出
这将产生以下代码 −
Pandas Index... Index(['Car', 'Bike', 'Shop', 'Car', 'Airplace', 'Truck'], dtype='object') 数组... ['Car' 'Bike' 'Shop' 'Car' 'Airplace' 'Truck'] dtype 对象... object 底层数据形状的元组... (6,)
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

