仅删除组内的重复项

我只想从数据框中删除特定子集中的重复项。在“A”列中的每个“规范”下,我想删除重复项,但我想在整个数据框中保留重复项(第一个“规范”下可能有一些行与第二个“规范”,但在“规范”下直到下一个“规范”我想删除重复项)


这是数据框


df


  A          B            C

  spec       first        second

  test       text1        text2

  act        text12       text13

  act        text14       text15

  test       text32       text33

  act        text34       text35

  test       text85       text86

  act        text87       text88

  test       text1        text2

  act        text12       text13

  act        text14       text15

  test       text85       text86

  act        text87       text88

  spec       third        fourth

  test       text1        text2

  act        text12       text13

  act        text14       text15

  test       text85       text86

  act        text87       text88

  test       text1        text2

  act        text12       text13

  act        text14       text15

  test       text85       text86

  act        text87       text88

这就是我想要的:


df


  A          B            C

  spec       first        second

  test       text1        text2

  act        text12       text13

  act        text14       text15

  test       text32       text33

  act        text34       text35

  test       text85       text86

  act        text87       text88

  spec       third        fourth

  test       text1        text2

  act        text12       text13

  act        text14       text15

  test       text85       text86

  act        text87       text88

我可以将数据帧拆分为“小”数据帧,然后在 for 循环中为每个“小”数据帧删除重复项,最后将它们连接起来,但我想知道是否还有其他解决方案。


我也尝试过并成功了:


dfList = df.index[df["A"] == "spec"].tolist()

dfList = np.asarray(dfList)

for dfL in dfList:

      idx = np.where(dfList == dfL)

      if idx[0][0]!=(len(dfList)-1):

            df.loc[dfList[idx[0][0]]:dfList[idx[0][0]+1]-1]

                     = df.loc[dfList[idx[0][0]]:dfList[idx[0][0]+1]-1].drop_duplicates()

      else:

            df.loc[dfList[idx[0][0]]:] = df.loc[dfList[idx[0][0]]:].drop_duplicates()

编辑:我必须将其添加到最后:


df.dropna(how='all', inplace=True)


但我只是想知道是否还有其他解决方案。


动漫人物
浏览 167回答 3
3回答

侃侃无极

这应该有效:df2 = df.drop_duplicates(subset=['A', 'B','C'])

湖上湖

使用groupby+ duplicated:df[~df.groupby(df.A.eq('spec').cumsum()).apply(lambda x: x.duplicated()).values]       A       B       C0   spec   first  second1   test   text1   text22    act  text12  text133    act  text14  text154   test  text32  text335    act  text34  text356   test  text85  text867    act  text87  text8813  spec   third  fourth14  test   text1   text215   act  text12  text1316   act  text14  text1517  test  text85  text8618   act  text87  text88细节我们使用cumsum. 组标签是:df.A.eq('spec').cumsum()0     11     12     13     14     15     16     17     18     19     110    111    112    113    214    215    216    217    218    219    220    221    222    223    2Name: A, dtype: int64然后在此系列上完成分组,并计算每组的重复项:df.groupby(df.A.eq('spec').cumsum()).apply(lambda x: x.duplicated()).valuesarray([False, False, False, False, False, False, False, False,  True,        True,  True,  True,  True, False, False, False, False, False,       False,  True,  True,  True,  True,  True])由此,剩下的就是保留对应于“False”的那些行(即不重复)。

狐的传说

另一个可能的解决方案可能是......您可以拥有一个计数器并从 A 列创建一个带有计数器值的新列,每当您在列值中遇到规范时,您就会增加计数器值。counter = 0def counter_fun(val):    if val == 'spec': counter+=1    return counterdf['new_col'] = df.A.apply(counter_fun)然后在 new_col 上分组,并删除重复项。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python