Python Pandas - 指示除首次出现之外的重复索引值


若要指示除首次出现之外的重复索引值,请使用 index.duplicated().keep 参数与值 first 一起使用。

首先,导入需要的库 −

import pandas as pd

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

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

显示索引 −

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

指示重复的索引值为 True,首个出现的值除外。将“keep”参数设置为“first” −

print("\nIndicating duplicate values except the first occurrence...\n", index.duplicated(keep='first'))

示例

以下是代码 −

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, except the first occurrence
# Set the "keep" parameter as "first"
print("\nIndicating duplicate values except the first occurrence...\n", index.duplicated(keep='first'))

输出

将生成以下代码 −

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

The dtype object...
object

Get the dimensions...
1

Indicating duplicate values except the first occurrence...
[False False False False True]

更新于: 13-10-2021

186 次浏览

开启你的 职业生涯

完成课程以获得认证

开始
广告