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 次瀏覽

開啟您的職業生涯

完成課程獲得認證

立即開始
廣告