拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何使用Python减少Pandas资料框中的重复元素

如何使用Python减少Pandas资料框中的重复元素

白鹭 - 2022-02-10 2132 0 0

我正在使用一个如下所示的资料框:

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提供了该中每个元素的出现次数

我怎样才能做到这一点?

谢谢你。

uj5u.com热心网友回复:

您可以使用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

uj5u.com热心网友回复:

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

df.groupby('H').count()
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *