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
#创建具有一些重复项的索引
index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])
#显示索引
print("Pandas Index with duplicates...\n",index)
#返回数据的dtype
print("\nThe dtype object...\n",index.dtype)
#获取数据的维度
print("\nGet the dimensions...\n",index.ndim)
#将重复的索引值指示为True,其余为False
#默认情况下,它保持未标记重复值的第一次出现
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]