Computer >> Máy Tính >  >> Lập trình >> Python

Python Pandas - Chỉ ra các giá trị chỉ mục trùng lặp ngoại trừ lần xuất hiện đầu tiên

Để chỉ ra các giá trị chỉ mục trùng lặp ngoại trừ lần xuất hiện đầu tiên, hãy sử dụng index.duplicated (). Sử dụng giữ lại tham số có giá trị đầu tiên.

Đầ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 với một số bản sao−

index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])

Hiển thị chỉ mục -

print("Pandas Index with duplicates...\n",index)

Chỉ ra các giá trị chỉ mục trùng lặp là True, ngoại trừ lần xuất hiện đầu tiên. Đặt thông số "keep" là "first" -

print("\nIndicating duplicate values except the first occurrence...\n", index.duplicated(keep='first'))

Ví dụ

Sau đây là mã -

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 duplicate index values as True, except the first occurrence
# Set the "keep" parameter as "first"
print("\nIndicating duplicate values except the first occurrence...\n", index.duplicated(keep='first'))

Đầu ra

Điều này sẽ tạo ra mã sau -

Pandas Index with duplicates...
Index(['Car', 'Bike', 'Airplane', 'Ship', 'Airplane'], dtype='object')

The dtype object...
object

Get the dimensions...
1

Indicating duplicate values except the first occurrence...
[False False False False True]