Python Pandas - 将所有重复的索引值标记为 True


若要将所有重复的索引值标记为 True,请使用 index.duplicated()。将 keep 参数的值设为 False

首先,导入所需的库 −

import pandas as pd

创建一些重复索引 −

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

显示索引 −

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

将所有重复的索引值标记为 True。将 "keep" 参数设为 "False" −

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

举例

代码如下 −

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 all duplicate index values as True
# Set the "keep" parameter as "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]

更新于: 13-Oct-2021

241 次浏览

启动你的 职业生涯

完成此课程获得认证

开始
广告