Python Pandas - 檢查索引是否具有唯一值


如需檢查索引是否具有唯一值,請使用index.is_unique

首先,匯入所需的庫 −

import pandas as pd

讓我們建立索引 −

index = pd.Index([50, 40, 30, 20, 10])

顯示索引 −

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

檢查索引是否具有唯一值 −

print("\nIs the Pandas index having unique values?\n",index.is_unique)

示例

以下是程式碼 −

import pandas as pd

# Creating the index
index = pd.Index([50, 40, 30, 20, 10])

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

# Return an array representing the data in the Index
print("\nArray...\n",index.values)

# Check if the index is having unique values
print("\nIs the Pandas index having unique values?\n",index.is_unique)

輸出

這將生成以下程式碼 −

Pandas Index...
Int64Index([50, 40, 30, 20, 10], dtype='int64')

Array...
[50 40 30 20 10]

Is the Pandas index having unique values?
True

更新於: 2021-10-13

3K+ 次瀏覽

啟動您的職業生涯

完成課程並獲得認證

開始
廣告