Python Pandas - 指出重複的索引值


要指出重複的索引值,請使用 index.duplicated() 方法。

首先,匯入所需的庫 −

import pandas as pd

建立帶有一些重複項的索引 −

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

顯示索引 −

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

將重複的索引值指示為 True,其餘為 False。預設情況下,它將重複值的第一次出現保持為未標記狀態 −

print("\nIndicating duplicate values...\n", index.duplicated())

示例

以下是程式碼 −

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, rest False
# By default it keeps the first occurrence of the duplicate value unmarked
print("\nIndicating duplicate values...\n", index.duplicated())

輸出

這將生成以下程式碼 −

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

The dtype object...
object

Get the dimensions...
1

Indicating duplicate values...
[False False False False True]

更新於:2021-10-13

682 次瀏覽

開啟 職業生涯

完成課程獲得認證

開始
廣告