Python Pandas - 除第一次出现外,删除重复值的返回索引
要返回除去第一次出现之外的重复值的索引,请使用该方法。将keep参数与valuefirst一起使用。index.drop_duplicates()
首先,导入所需的库-
import pandas as pd
创建具有一些重复项的索引-
index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])
显示索引-
print("Pandas Index with duplicates...\n",index)删除重复值的返回索引。值为“first”的“keep”参数保留每组重复条目的第一次出现-
index.drop_duplicates(keep='first')
示例
以下是代码-
import pandas as pd
#创建具有一些重复项的索引
index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])
#显示索引
print("Pandas Index with duplicates...\n",index)
#返回数据的dtype
print("\nThe dtype object...\n",index.dtype)
#获取数据中的字节
print("\nGet the bytes...\n",index.nbytes)
#获取数据的维度
print("\nGet the dimensions...\n",index.ndim)
#删除重复值的返回索引
# The "keep" 带值的参数 "first" keeps the first occurrence for each set of duplicated entries
print("\nIndex with duplicate values removed (keeping the first occurrence)...\n",index.drop_duplicates(keep='first'))输出结果这将产生以下代码-
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 first occurrence)... Index(['Car', 'Bike', 'Airplane', 'Ship'], dtype='object')
热门推荐
10 66红包祝福语简短的
11 用名字做祝福语简短
12 七十年祝福语简短
13 小升初送给儿子祝福语简短
14 哈尼族祝福语简短
15 祝福语男性朋友简短大方
16 新年祝福语给长辈简短
17 结婚祝福语简短精彩短句
18 企业新春祝福语简短大全