首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用Python减少Pandas数据中的重复元素

如何用Python减少Pandas数据中的重复元素
EN

Stack Overflow用户
提问于 2021-12-25 00:03:36
回答 2查看 67关注 0票数 0

我正在处理一个数据文件,它看起来像这样:

代码语言:javascript
复制
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。我想修改这个dataframe,以便获得如下内容:

代码语言:javascript
复制
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

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

我怎么能这么做?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-25 00:13:08

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

代码语言:javascript
复制
df = df.drop_duplicates(subset='H').assign(I=df.groupby('H')['H'].transform('count'))

输出:

代码语言:javascript
复制
>>> 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
票数 1
EN

Stack Overflow用户

发布于 2021-12-25 00:09:23

我们可以使用一个groupby并对元素进行如下计数:

代码语言:javascript
复制
df.groupby('H').count()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70477243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档