Python Pandas - 返回已删除重复值的索引,同时保留最后一次出现
要返回已删除重复值的索引,同时保留最后一次出现,请使用index.drop_duplicates() 方法。使用具有值last 的keep 参数。
首先,导入所需的库 -
import pandas as pd
使用一些重复创建索引
index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])
显示索引 -
print("Pandas Index with duplicates...\n",index)返回已删除重复值的索引。“keep”参数值“last”为每组重复条目保留最后一次出现 -
print("\nIndex with duplicate values removed (keeping the last occurrence)...\n",index.drop_duplicates(keep='last'))
示例
以下是代码 -
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 "last" keeps the last occurrence for each set of duplicated entries
print("\nIndex with duplicate values removed (keeping the last occurrence)...\n",index.drop_duplicates(keep='last'))输出
这将生成以下代码 -
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 (keeping the last occurrence)... Index(['Car', 'Bike', 'Ship', 'Airplane'], dtype='object')
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP