Python - 检查 Pandas 索引是否是对象数据类型


如需检查 Pandas 索引是否是对象数据类型,请使用 index.is_object() 方法。首先,导入所需的库 -

import pandas as pd

创建 Pandas 索引 -

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

显示 Pandas 索引 -

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

检查索引值是否为对象数据类型 -

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

更新于: 13-10-2021

604 次浏览

开启您的职业生涯

完成课程获得认证

立即开始
广告