Python Pandas - 返回完全删除重复值的索引


要返回完全删除重复值的索引,请使用 index.drop_duplicates() 方法。

首先,导入所需库 −

import pandas as pd

创建包含一些重复数据的索引 −

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

显示索引 −

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

返回删除了重复值的索引。值为 "False" 的 "keep" 参数将丢弃每组重复项的所有出现 −

print("\nIndex with duplicate values removed (drops all occurrences)...\n",
index.drop_duplicates(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 bytes in the data
print("\nGet the bytes...\n",index.nbytes)

# get the dimensions of the data
print("\nGet the dimensions...\n",index.ndim)

# Return Index with duplicate values removed
# The "keep" parameter with value "False" drops all occurrences for each set of duplicated entries
print("\nIndex with duplicate values removed (drops all occurrences)...\n",
index.drop_duplicates(keep = False))

输出

将生成以下代码 −

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

The dtype object...
object

Get the bytes...
40

Get the dimensions...
1

Index with duplicate values removed (drops all occurrences)...
Index(['Car', 'Bike', 'Ship'], dtype='object')

更新于: 13-10-2021

197 浏览

开启你的 事业

完成课程获得认证

开始
广告
© . All rights reserved.