为表中的列使用唯一值

2024-04-26 10:54:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我在pandas中有一个数据框,它有五列:contig、length、identity、percent和hit。该数据从BLAST输出中解析,并按重叠长度和匹配百分比排序。我的目标是让输出只为每个唯一的重叠写一行。输出的一个示例:

   contig        length  identity     percent  hit                                                                             
   contig-100_0  5485    [1341/1341]  [100.%]  ['hit1']
   contig-100_0  5485    [5445/5445]  [100.%]  ['hit2']
   contig-100_0  5485        [59/59]  [100.%]  ['hit3']
   contig-100_1  2865    [2865/2865]  [100.%]  ['hit1']
   contig-100_2  2800    [2472/2746]  [90.0%]  ['hit1']
   contig-100_3  2417    [2332/2342]  [99.5%]  ['hit1']
   contig-100_4  2204    [2107/2107]  [100.%]  ['hit1']
   contig-100_4  2000    [1935/1959]  [98.7%]  ['hit2']

我希望上面看起来像这样:

   contig        length  identity     percent  hit                                                                             
   contig-100_0  5485    [1341/1341]  [100.%]  ['hit1']
   contig-100_1  2865    [2865/2865]  [100.%]  ['hit1']
   contig-100_2  2800    [2472/2746]  [90.0%]  ['hit1']
   contig-100_3  2417    [2332/2342]  [99.5%]  ['hit1']
   contig-100_4  2204    [2107/2107]  [100.%]  ['hit1']

以下是我用于生成上述输出的代码:

df = pd.read_csv(path+i,sep='\t', header=None, engine='python', \ 
     names=['contig','length','identity','percent','hit'])
df = df.sort_values(['length', 'percent'], ascending=[False, False])
top_hits = df.to_string(justify='left',index=False)
with open ('sorted_contigs', 'a') as sortedfile:
    sortedfile.write(top_hits+"\n")

我知道pandas中的unique()方法,并且认为我需要使用的语法是df.contig.unique(),但是我不确定我将把它放在代码中的什么地方。我仍在学习熊猫,因此非常感谢您的帮助!多谢各位


Tags: 数据代码falsepandasdftoplengthidentity
1条回答
网友
1楼 · 发布于 2024-04-26 10:54:07

您可以使用DataFrame.groupby(<colname>).head(<num_of_rows>)执行此操作:

df.groupby('contig').head(1)

以及输出:

          contig    length  identity    percent hit
0   contig-100_0    5485    [1341/1341] [100.%] ['hit1']
3   contig-100_1    2865    [2865/2865] [100.%] ['hit1']
4   contig-100_2    2800    [2472/2746] [90.0%] ['hit1']
5   contig-100_3    2417    [2332/2342] [99.5%] ['hit1']
6   contig-100_4    2204    [2107/2107] [100.%] ['hit1']

相关问题 更多 >