Python Pandas - 指出重复的索引值


要指出重复的索引值,请使用 index.duplicated() 方法。

首先,导入所需的库 −

import pandas as pd

创建带有一些重复项的索引 −

index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])

显示索引 −

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

将重复的索引值指示为 True,其余为 False。默认情况下,它将重复值的第一次出现保持为未标记状态 −

print("\nIndicating duplicate values...\n", index.duplicated())

示例

以下是代码 −

import pandas as pd

# Creating the index with some duplicates
index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])

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

# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)

# get the dimensions of the data
print("\nGet the dimensions...\n",index.ndim)

# Indicate duplicate index values as True, rest False
# By default it keeps the first occurrence of the duplicate value unmarked
print("\nIndicating duplicate values...\n", index.duplicated())

输出

这将生成以下代码 −

Pandas Index with duplicates...
Index(['Car', 'Bike', 'Airplane', 'Ship', 'Airplane'], dtype='object')

The dtype object...
object

Get the dimensions...
1

Indicating duplicate values...
[False False False False True]

更新于:2021-10-13

682 次浏览

开启 职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.