Python Pandas - 检查索引是否有重复的值
要检查索引是否有重复的值,请在 Pandas 中使用 index.has_duplicates 属性。
首先,导入所需的库 −
import pandas as pd
创建索引 −
index = pd.Index(['Car','Bike','Truck','Car','Airplane'])
显示索引 −
print("Pandas Index...\n",index)
检查索引是否有重复值 −
print("\nIs the Pandas index having duplicate values?\n",index.has_duplicates)
例子
以下为代码 −
import pandas as pd # Creating the index index = pd.Index(['Car','Bike','Truck','Car','Airplane']) # Display the index print("Pandas Index...\n",index) # Return an array representing the data in the Index print("\nArray...\n",index.values) # Check if the index is having duplicate values print("\nIs the Pandas index having duplicate values?\n",index.has_duplicates)
输出
这将产生以下代码 −
Pandas Index... Index(['Car', 'Bike', 'Truck', 'Car', 'Airplane'], dtype='object') Array... ['Car' 'Bike' 'Truck' 'Car' 'Airplane'] Is the Pandas index having duplicate values? True
广告