Search code examples
pythonlookup-tables

Python - have list and want to pull values from a table and put in new table


Hello I am very new to python and apologize for asking something that may be silly. Any reference in the right direction is very much appreciated.

I have a list of strings (3500 gene names)

I have a table that contains 35000 genes along with several characteristics of those genes. Ie. Gene_ID Gene_Name chr_loc FPKM1 FPKM2 LFC ...etc

I want to find the genes in my list and pull out two values associated with those genes and place these three things for all 3500 genes in a new table.

I really appreciate any help!


Solution

  • Assuming the headers are in the file, here is the basic idea:

    import csv
    namelist = ['name1', 'name2']
    fout = open('out.csv','w')
    fout.write('header1,header2,header3')
    fin = open('temp.csv','r')
    reader = csv.DictReader(fin)
    for row in reader:
      if row['header1'] in namelist:
          fout.write(newrow(row))
    

    You will of course need to write a newrow function and adjust details for your input file format.