Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas read_table usecols error with ":"

I am trying to read a certain range of nonconsecutive columns from my file using python pandas read_table function. To that end, I am trying:

df=pd.read_table('genes.fpkm_trackingTest', usecols=[0:4, 8,9, 12:19])

The idea is that i am trying to use ":" to select the range of number of columns for usecols, rather than using column numbers separated with comma ",". I am getting a syntax error. If I use commas "," to separate column numbers, then it works fine.

df=pd.read_table('genes.fpkm_trackingTest', usecols=[0,1,2,4, 8,9, 12,13,14,15,16,17,18,19])

However, that can be cumbersome as sometimes i have to select 40 columns. How can i come around this?

I even tried

usecols=[range(0:4), 8, 9, range(12:19)]

but it also gave me errors.

I think that should be simple thing to solve but I couldnt find a solution online.

like image 905
BioProgram Avatar asked Feb 15 '16 03:02

BioProgram


1 Answers

It should be...

Python 3:

usecols = [*range(0, 5), 8, 9, *range(12, 20)]

Python 2:

usecols = range(0, 5) + [8, 9] + range(12, 20)

Hope it helps!

like image 166
cdonts Avatar answered Sep 20 '22 21:09

cdonts