หากต้องการตรวจสอบว่าดัชนีมีค่าที่ซ้ำกันหรือไม่ ให้ใช้ 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