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+ 次浏览

启动您的职业生涯

完成课程并获得认证

开始
广告