首页 文章资讯内容详情

Python Pandas - 将所有重复的索引值指示为 True

2026-06-03 1 花语

要将所有重复的索引值指示为True,请使用.使用值为Falsekeep参数index.duplicated()

首先,导入所需的库-

import pandas as pd

创建具有一些重复项的索引-

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

显示索引-

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

将所有重复的索引值指示为True。将“保持”参数设置为“假”-

print("\nIndicating all duplicate index values True...\n", index.duplicated(keep=False))

示例

以下是代码-

import pandas as pd #Creatingtheindexwithsomeduplicates index = pd.Index([Car,Bike,Airplane,Ship,Airplane]) #Displaytheindex print("Pandas Index with duplicates...\n",index) #Returnthedtypeofthedata print("\nThe dtype object...\n",index.dtype) #getthedimensionsofthedata print("\nGet the dimensions...\n",index.ndim) #IndicateallduplicateindexvaluesasTrue # Set the "keep" 参数为 "False" print("\nIndicating all duplicate index values True...\n", index.duplicated(keep=False))输出结果

这将产生以下代码-

Pandas Index with duplicates... Index([Car, Bike, Airplane, Ship, Airplane], dtype=object) The dtype object... object Get the dimensions... 1 Indicating all duplicate index values True... [False False True False True]