Question: How read a CSV file skipping the first column using python
0
gravatar for mandimunari
7 days ago by
mandimunari0 wrote:

I'm checking the presence of genes in at least 95% of the analyzed bacteria, and to do this is necessary read a CSV file using python. This file contains 15 columns corresponding to the name of the bacteria, and the rows is about the presence (value >= 1) or absence (value <= 0) of the genes, but it's necessary skip the first column and pass through each row of each column and return if the value of the lines is <=0 or >=1. So, I'm stuck in how to skip the first column using CSV library in python.

Thanks

python genome • 117 views
ADD COMMENTlink modified 7 days ago by steve1.9k • written 7 days ago by mandimunari0
1

You'd be best off working on this data in the pandas module. What you're asking is an XY problem. You don't need to ignore the first column specifically, you just need to query the relevant column of the data table. i.e.: print all rows of the dataframe where column X =< 0 && X >= 1.

You should give this a go first, as its not a difficult problem with the right tools, and being able to perform queries like this on a dataframe is a very powerful skill.

Make an attempt, show us what code you come up with, and we'll be happy to help you further.

ADD REPLYlink written 7 days ago by jrj.healey9.8k

Please show us what you tried. This is also more programming than bioinformatics and might get closed for that reason.

ADD REPLYlink modified 7 days ago • written 7 days ago by WouterDeCoster35k
2
gravatar for steve
7 days ago by
steve1.9k
United States
steve1.9k wrote:

To skip the first column:

$ printf 'foo,bar\n1,2\n' > test.csv

>>> import csv
>>> with open('test.csv') as fin:
...     reader = csv.reader(fin)
...     for row in reader:
...             print(row[1:])
...
['bar']
['2']

To skip the first row:

$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> rows_to_keep = []
>>> with open('test.csv') as fin:
...     next(fin)
...     reader = csv.reader(fin)
...     for row in reader: rows_to_keep.append(row)
...
'foo,bar\n'
>>> print(rows_to_keep)
[['1', '2']]

I think there might be a slight syntax difference in the next function between Python 2 and 3 but this is the gist of it.

See also: https://stackoverflow.com/questions/14257373/skip-the-headers-when-editing-a-csv-file-using-python

ADD COMMENTlink modified 7 days ago • written 7 days ago by steve1.9k
1

OP asked for skipping the first column, not row, but I'm willing to bet they meant row...

ADD REPLYlink written 7 days ago by jrj.healey9.8k

thanks I updated it

ADD REPLYlink written 7 days ago by steve1.9k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1436 users visited in the last hour