简体   繁体   中英

selection of one row based on other columns in python

small part of my csv file is like the following lines:

481116  ABCF3   466 0   ENSG00000161204 0
485921  ABCF3   466 0   ENSG00000161204 0
489719  ABCF3   466 0   ENSG00000161204 0
498136  ABCF3   466 2   ENSG00000161204 0.0019723866
273359  ABHD10  326 78  ENSG00000144827 0.0301158301
491580  ABHD10  326 0   ENSG00000144827 0
493784  ABHD10  326 0   ENSG00000144827 0
494817  ABHD10  326 1   ENSG00000144827 0.0012484395

the columns are separated by "," in the file. in the 2nd column there are many repeated ids and I would like to select only one of the ids based the values in the 6th column. in other word, for each id I want to choose the one with the highest number in the column 6. the results for the mentioned part, must be like this.

498136  ABCF3   466 2   ENSG00000161204 0.0019723866
273359  ABHD10  326 78  ENSG00000144827 0.0301158301

I have tried to make it in python and wrote some pieces of codes in the following framework but non of them worked:

with open('data.csv') as f, open('out.txt', 'w') as out:
    line = [line.split(',')for line in f]
    .
    .
    out.write(','.join(results))

you_data.csv:

481116,ABCF3, 466,0, ENSG00000161204,0
485921,ABCF3, 466,0, ENSG00000161204,0
489719,ABCF3, 466,0, ENSG00000161204,0
498136,ABCF3, 466,2, ENSG00000161204,0.0019723866
273359,ABHD10,326,78,ENSG00000144827,0.0301158301
491580,ABHD10,326,0, ENSG00000144827,0
493784,ABHD10,326,0, ENSG00000144827,0
494817,ABHD10,326,1, ENSG00000144827,0.0012484395  

code:

import csv
from collections import defaultdict

with open('you_data.csv', newline='') as f, open('out.csv', 'w', newline='') as out:
    f_reader = csv.reader(f)
    out_writer = csv.writer(out)
    d = defaultdict(list)
    for line in f_reader:
        d[line[1]].append(line)
    for _,v in d.items():
        new_line = sorted(v, key=lambda i:float(i[5]), reverse=True)[0]
        out_writer.writerow(new_line)

out.csv:

498136,ABCF3, 466,2, ENSG00000161204,0.0019723866
273359,ABHD10,326,78,ENSG00000144827,0.0301158301

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM