【问题标题】:How to reduce repeated elements in a Pandas dataframe with Python如何使用 Python 减少 Pandas 数据框中的重复元素
【发布时间】:2022-01-25 08:12:27
【问题描述】:

我正在使用如下所示的数据框:

A                       B           C       D       E   F   G   H
ctg.s1.000000F_arrow    CDS gene    21215   22825   0   +   .   DAFEIOHN_00017
ctg.s1.000000F_arrow    CDS gene    21215   22825   0   +   .   DAFEIOHN_00017
ctg.s1.000000F_arrow    CDS gene    64501   66033   0   -   .   DAFEIOHN_00049
ctg.s1.000000F_arrow    CDS gene    70234   78846   0   +   .   DAFEIOHN_00053
ctg.s1.000000F_arrow    CDS gene    103455  106526  0   +   .   DAFEIOHN_00074
ctg.s1.000000F_arrow    CDS gene    161029  161712  0   +   .   DAFEIOHN_00132
ctg.s1.000000F_arrow    CDS gene    170711  171520  0   +   .   DAFEIOHN_00142
ctg.s1.000000F_arrow    CDS gene    203959  204450  0   -   .   DAFEIOHN_00174
ctg.s1.000000F_arrow    CDS gene    211381  212196  0   +   .   DAFEIOHN_00184
ctg.s1.000000F_arrow    CDS gene    236673  238499  0   +   .   DAFEIOHN_00209
ctg.s1.000000F_arrow    CDS gene    533077  533850  0   +   .   DAFEIOHN_00475
ctg.s1.000000F_arrow    CDS gene    533995  535194  0   +   .   DAFEIOHN_00572
ctg.s1.000000F_arrow    CDS gene    641146  643083  0   +   .   DAFEIOHN_00572

如您所见,H 列中有重复的元素,例如 DAFEIOHN_00017DAFEIOHN_00572。我想修改这个数据框以获得这样的东西:

A                       B           C       D       E   F   G   H                I
ctg.s1.000000F_arrow    CDS gene    21215   22825   0   +   .   DAFEIOHN_00017   2
ctg.s1.000000F_arrow    CDS gene    64501   66033   0   -   .   DAFEIOHN_00049   1
ctg.s1.000000F_arrow    CDS gene    70234   78846   0   +   .   DAFEIOHN_00053   1
ctg.s1.000000F_arrow    CDS gene    103455  106526  0   +   .   DAFEIOHN_00074   1
ctg.s1.000000F_arrow    CDS gene    161029  161712  0   +   .   DAFEIOHN_00132   1
ctg.s1.000000F_arrow    CDS gene    170711  171520  0   +   .   DAFEIOHN_00142   1
ctg.s1.000000F_arrow    CDS gene    203959  204450  0   -   .   DAFEIOHN_00174   1
ctg.s1.000000F_arrow    CDS gene    211381  212196  0   +   .   DAFEIOHN_00184   1
ctg.s1.000000F_arrow    CDS gene    236673  238499  0   +   .   DAFEIOHN_00209   1
ctg.s1.000000F_arrow    CDS gene    533077  533850  0   +   .   DAFEIOHN_00475   1
ctg.s1.000000F_arrow    CDS gene    533995  535194  0   +   .   DAFEIOHN_00572   2

在第二个数据框中,重复的元素仅显示一次,并且有一个新列 I,其中提供了 H 列的每个元素的出现次数。

我该怎么做?

谢谢。

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    您可以使用drop_duplicates 删除在特定列中重复的行,并使用assign 创建一个新列,其中包含从groupby('H')transform('count') 的组合返回的值以确定每个列的计数H 的唯一值:

    df = df.drop_duplicates(subset='H').assign(I=df.groupby('H')['H'].transform('count'))
    

    输出:

    >>> df
                           A         B       C       D  E  F  G               H  I
    0   ctg.s1.000000F_arrow  CDS-gene   21215   22825  0  +  .  DAFEIOHN_00017  2
    2   ctg.s1.000000F_arrow  CDS-gene   64501   66033  0  -  .  DAFEIOHN_00049  1
    3   ctg.s1.000000F_arrow  CDS-gene   70234   78846  0  +  .  DAFEIOHN_00053  1
    4   ctg.s1.000000F_arrow  CDS-gene  103455  106526  0  +  .  DAFEIOHN_00074  1
    5   ctg.s1.000000F_arrow  CDS-gene  161029  161712  0  +  .  DAFEIOHN_00132  1
    6   ctg.s1.000000F_arrow  CDS-gene  170711  171520  0  +  .  DAFEIOHN_00142  1
    7   ctg.s1.000000F_arrow  CDS-gene  203959  204450  0  -  .  DAFEIOHN_00174  1
    8   ctg.s1.000000F_arrow  CDS-gene  211381  212196  0  +  .  DAFEIOHN_00184  1
    9   ctg.s1.000000F_arrow  CDS-gene  236673  238499  0  +  .  DAFEIOHN_00209  1
    10  ctg.s1.000000F_arrow  CDS-gene  533077  533850  0  +  .  DAFEIOHN_00475  1
    11  ctg.s1.000000F_arrow  CDS-gene  533995  535194  0  +  .  DAFEIOHN_00572  2
    

    【讨论】:

      【解决方案2】:

      我们可以使用groupby 并像这样计算元素:

      df.groupby('H').count()
      

      【讨论】:

        猜你喜欢
        • 2020-10-04
        • 2018-09-11
        • 2019-09-28
        • 2020-11-12
        • 2019-02-27
        • 2021-11-09
        • 1970-01-01
        • 2013-09-04
        • 2023-03-06
        相关资源
        最近更新 更多