หากต้องการระบุค่าดัชนีที่ซ้ำกันทั้งหมดเป็น True ให้ใช้ index.duplicated() . ใช้ เก็บ พารามิเตอร์ที่มีค่า เท็จ
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
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]