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
,并尝试使用长度函数,但是我一直在输出中同时获取两行。
答案 0 :(得分:1)
尝试:
# 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)