Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • upendra_35
    Senior Member
    • Apr 2010
    • 102

    #1

    how do i filter rownames based on column value

    I have dataframe with two columns (target_id and fpkm). I want to keep only those in first column that are not duplicated. If they are duplicated then i would like to keep only one based on value in column 2. I have given an example for this kind below.

    target_id fpkm
    comp247393_c0_seq1 3.197885
    comp257058_c0_seq4 1.624577
    comp242590_c0_seq1 1.750319
    comp77911_c0_seq1 1.293059
    comp241426_c0_seq1 1.626589
    comp288413_c0_seq1 14.828853
    comp294436_c0_seq1 11.555596
    comp63603_c0_seq1 1.982386
    comp267138_c0_seq1 8.594494
    comp267138_c0_seq2 11.134958
    comp321623_c0_seq1 6.934149

    In the above dataframe as you can see there are two rownames with the same name (almost) comp267138_c0_seq1 comp267138_c0_seq2 and i want to keep only comp267138_c0_seq2 because it has higher value in column 2. Please help me with this....
  • rflrob
    Member
    • May 2010
    • 50

    #2
    Assuming you want to keep the seq number, it could be done with a moderately simple python script:
    Code:
    fh = open('file_name')
    print fh.readline() # Clear the header
    best_lines = {}
    for line in fh:
        id, fpkm = line.strip().split()
        fpkm = float(fpkm)  # Turn into a number
        id_base, id_seqnum = id.rsplit('_', 1) # Assume everything before _seq is the same
    
        if id_base not in best_lines:
            best_lines[id_base] = (fpkm, id_seqnum)
        else:
            if fpkm > best_lines[id_base][0]:
                best_lines[id_base] = (fpkm, id_seqnum)
    
    for id_base in best_lines:
        fpkm, id_seqnum = best_lines[id_base]
        print id_base+"_"+id_seqnum, fpkm

    This won't necessarily retain the original order of the file, but will deal with the possibility that, for instance, comp267138_c0_seq1 and comp267138_c0_seq2 aren't in adjacent lines.

    Comment

    • upendra_35
      Senior Member
      • Apr 2010
      • 102

      #3
      Originally posted by rflrob View Post
      Assuming you want to keep the seq number, it could be done with a moderately simple python script:
      Code:
      fh = open('file_name')
      print fh.readline() # Clear the header
      best_lines = {}
      for line in fh:
          id, fpkm = line.strip().split()
          fpkm = float(fpkm)  # Turn into a number
          id_base, id_seqnum = id.rsplit('_', 1) # Assume everything before _seq is the same
      
          if id_base not in best_lines:
              best_lines[id_base] = (fpkm, id_seqnum)
          else:
              if fpkm > best_lines[id_base][0]:
                  best_lines[id_base] = (fpkm, id_seqnum)
      
      for id_base in best_lines:
          fpkm, id_seqnum = best_lines[id_base]
          print id_base+"_"+id_seqnum, fpkm

      This won't necessarily retain the original order of the file, but will deal with the possibility that, for instance, comp267138_c0_seq1 and comp267138_c0_seq2 aren't in adjacent lines.
      Hi rflrob, it worked perfectly. I have been struggling to write something like this in perl for a while but couldn't get it to work and your script worked like a charm. Don't worry about the order of id's as i am not too worried about them as long as i filter the columns. Thanks a lot again man.

      Comment

      Latest Articles

      Collapse

      • SEQadmin2
        Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
        by SEQadmin2



        Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

        Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
        ...
        10-14-2026, 09:55 AM
      • SEQadmin2
        New Genomics Technologies Take Aim at Long-Standing Limits
        by SEQadmin2


        Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

        We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
        ...
        09-28-2026, 10:25 AM

      ad_right_rmr

      Collapse

      News

      Collapse

      Topics Statistics Last Post
      Started by SEQadmin2, 10-22-2026, 11:13 AM
      0 responses
      23 views
      0 reactions
      Last Post SEQadmin2  
      Started by SEQadmin2, 10-22-2026, 10:48 AM
      0 responses
      20 views
      0 reactions
      Last Post SEQadmin2  
      Started by SEQadmin2, 10-15-2026, 10:52 AM
      0 responses
      36 views
      0 reactions
      Last Post SEQadmin2  
      Started by SEQadmin2, 10-14-2026, 05:39 AM
      0 responses
      42 views
      0 reactions
      Last Post SEQadmin2  
      Working...