Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用值计数对数据帧进行排序_Python_Pandas - Fatal编程技术网

Python 使用值计数对数据帧进行排序

Python 使用值计数对数据帧进行排序,python,pandas,Python,Pandas,我有一个数据框,在“component_id”列下,我有一个component_id重复多次。 以下是df的外观: In [82]: df.head() Out[82]: index molregno chembl_id assay_id tid tid component_id 0 0 942606 CHEMBL1518722 688422 103668 103668 4891 1 0 94

我有一个数据框,在“component_id”列下,我有一个component_id重复多次。 以下是df的外观:

In [82]: df.head()
Out[82]:
   index  molregno      chembl_id  assay_id     tid     tid  component_id  
0      0    942606  CHEMBL1518722    688422  103668  103668          4891
1      0    942606  CHEMBL1518722    688422  103668  103668          4891
2      0    942606  CHEMBL1518722    688721      78      78           286
3      0    942606  CHEMBL1518722    688721      78      78           286
4      0    942606  CHEMBL1518722    688779  103657  103657          5140

  component_synonym
0              LMN1
1              LMNA
2              LGR3
3              TSHR
4              MAPT
可以看出,相同的组件id可以链接到不同的组件同义词(本质上是相同的基因,但名称不同)。我想找出每个基因的频率,因为我想找出前20个最常被击中的基因,因此,我在“component_id”列上执行了一个值_counts。我得到了这样的东西

In [84]: df.component_id.value_counts()
Out[84]:
5432    804
3947    402
5147    312
3       304
2693    294
75      282
Name: component_id, dtype: int64
我有没有办法根据出现次数最多的组件id来订购整个数据帧? 还有,我的数据帧是否可能只包含每个组件的第一个id


任何建议都将不胜感激

我认为您可以使用count对行进行排序,然后删除count列,即

df['count'] = df.groupby('component_id')['component_id'].transform('count')
df_sorted = df.sort_values(by='count',ascending=False).drop('count',1)

这是有效的,在这一步之后,我删除了基于组件id的重复行。谢谢!