首页 > 解决方案 > 根据数据框列中的值删除重复项

问题描述

Senario         
        


key              associated_keys                                    value      associated_value
KP6070  KP706010/KP706020/KP706030/KP706040/KP706050/KP706060/   AFE.706070.KP    AFE.706010.RT
KP6650  KP706610/KP706620//KP706630/KP706640/KP706650            AFE.706650.KP    AFE.706010.RT

我试过的python脚本。

Deduptest.groupby(['associated_keys']).max()['associated_value'].reset_index()


Deduptest.drop_duplicates(['associated_value'],keep= 'first')

预计出局

key                     associated_keys                               value    associated_value
KP6070  KP706010/KP706020/KP706030/KP706040/KP706050/KP706060/   AFE.706070.KP    AFE.706010.RT

我正在尝试根据associated_value列和删除重复项associated_keys。如果该associated_keys列中的任何其他行中的值已经存在,并且对于这两个行,如果associated_value列数据相同,那么我想要其中具有最高长度或更多数据的行。

我尝试drop_duplicates并尝试使用长度函数,但我一直在输出中获取两行。

标签: pythondataframe

解决方案


尝试:

# set up the key to get proper order
df["sort_key"]=df["associated_keys"].str.len()

# sort by that key
df.sort_values("sort_key", inplace=True, ascending=False)

# drop, keeping only the first record (in sorted dataframe, so the one with highest Len)
df.drop_duplicates(subset="associated_value", keep="first", inplace=True)

# drop sort column
df.drop("sort_key", axis=1, inplace=True)

推荐阅读