Python Pandas - 將所有重複的索引值標記為 True


若要將所有重複的索引值標記為 True,請使用 index.duplicated()。將 keep 引數的值設為 False

首先,匯入所需的庫 −

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]

更新於: 13-Oct-2021

241 次瀏覽

啟動你的 職業生涯

完成此課程獲得認證

開始
廣告
© . All rights reserved.