I have a large csv file with approximately 170 columns’ worth of numerical data (the columns) for about 1000 individuals (the rows). What I’d like to do is pipe a particular value for a particular case from the csv file into a text file.
More specifically, my csv file begins with a column of ID numbers (cases), and about 170 columns containing various numerical datapoints for each case. I want to iterate through the csv file and pull column 17 (as an example) for “CASE156” (as an example), and save the data at that location as:
CASE156_column_17.txt
To initially read in the data, I am using the Python csv builtin as:
f =csv.reader(open('genotypes.csv','rbU'))
I have tried
rownum=0
for row in f:
if rownum=='CASE156':
print column[16]
However, this does not produce the desired result. How can I can pull a specific column’s data for a specific case?
To make matters slightly more complicated, if there is NO data at that cell, I would like to skip it and NOT produce a .txt file.
Many thanks in advance,
Let’s assume that the key you are looking for is located in column ‘key_index’
then you can use:
Reading basic Python documentation appreciated.
You can also use
DictReadermaking the CSV data a bit more approachable through a dictinstead of a row that you can only access by column number.
http://docs.python.org/2/library/csv.html#csv.DictWriter