เพื่อบ่งชี้ค่า Index ที่ซ้ำทั้งหมดเป็น True ให้ใช้เมธอด index.duplicated() ใช้พารามิเตอร์ keep กับค่า False
เริ่มต้นด้วยการนำเข้าไลบรารีที่จำเป็น:
import pandas as pd
สร้าง Index ที่มีค่าบางส่วนซ้ำกัน:
index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])
แสดง Index:
print("Pandas Index with duplicates...\n", index)
บ่งชี้ค่า 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]