python - 仅删除组内的重复项

标签 python pandas dataframe group-by pandas-groupby

我只想从数据框中删除特定子集中的重复项。在“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)

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

最佳答案

使用groupby + 重复:

df[~df.groupby(df.A.eq('spec').cumsum()).apply(lambda x: x.duplicated()).values]

       A       B       C
0   spec   first  second
1   test   text1   text2
2    act  text12  text13
3    act  text14  text15
4   test  text32  text33
5    act  text34  text35
6   test  text85  text86
7    act  text87  text88
13  spec   third  fourth
14  test   text1   text2
15   act  text12  text13
16   act  text14  text15
17  test  text85  text86
18   act  text87  text88
<小时/>

详细信息

我们使用cumsum查找特定“spec”条目下的所有行。组标签是:

df.A.eq('spec').cumsum()

0     1
1     1
2     1
3     1
4     1
5     1
6     1
7     1
8     1
9     1
10    1
11    1
12    1
13    2
14    2
15    2
16    2
17    2
18    2
19    2
20    2
21    2
22    2
23    2
Name: A, dtype: int64

然后对该系列进行分组,并计算每组的重复项:

df.groupby(df.A.eq('spec').cumsum()).apply(lambda x: x.duplicated()).values

array([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”对应的行(即,重复)。

关于python - 仅删除组内的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53929947/

相关文章:

python - 使用模数运算符时,我希望数字不是余数 0

python - h5py 使用指针的对称数组

python - 2 个不同的文本 block 合并在一起。如果我知道 1 是什么,我可以将它们分开吗?

python - 跳过在 python 中绘制 NaN 和 inf 值

Python使用列表中的项目迭代创建过滤器表达式

python - 将复杂的数据帧行划分为 Pyspark 中的简单行

pandas 中的正则表达式根据另一列中的字符串查找匹配项

python - multiprocessing.Value 无法正确存储 float

python - pandas 将列转为行

python - 迭代更改 Pandas 数据框列中的每个单元格