Để kiểm tra xem chỉ mục có các giá trị trùng lặp hay không, hãy sử dụng index.has_duplicates tài sản ở Pandas.
Đầu tiên, hãy nhập các thư viện được yêu cầu -
import pandas as pd
Tạo chỉ mục -
index = pd.Index(['Car','Bike','Truck','Car','Airplane'])
Hiển thị chỉ mục -
print("Pandas Index...\n",index) Kiểm tra xem chỉ mục có các giá trị trùng lặp hay không -
print("\nIs the Pandas index having duplicate values?\n",index.has_duplicates)
Ví dụ
Sau đây là mã -
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) Đầu ra
Điều này sẽ tạo ra mã sau -
Pandas Index... Index(['Car', 'Bike', 'Truck', 'Car', 'Airplane'], dtype='object') Array... ['Car' 'Bike' 'Truck' 'Car' 'Airplane'] Is the Pandas index having duplicate values? True