Python Pandas - 使用 0 個元素檢查索引是否為空


要檢查索引是否為空且包含 0 個元素,請在 Pandas 中使用 index.empty 屬性。

首先,匯入所需的庫 −

import pandas as pd

建立索引 −

index = pd.Index([])

顯示索引 −

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

檢查空索引 −

print("\nIs the index empty?\n",index.empty)

示例

以下是程式碼 −

import pandas as pd

# Creating the index
index = pd.Index([])

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

# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)

# check for empty index
print("\nIs the index empty?\n",index.empty)

輸出

將生成以下程式碼 −

Pandas Index...
Index([], dtype='object')

Number of elements in the index...
0

Is the index empty?
True

更新時間:2021-10-13

2 千次瀏覽

開啟你的 職業生涯

完成課程的學習,獲得認證

開始
廣告