Pandas read_table fixes error when using ":"

I am trying to read a specific range of non-matching columns from my file using the python pandas read_table function. To this end, I try:

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

The idea is that I'm trying to use ":" to select a range of column numbers for usecols, rather than using column numbers separated by comma ",". I get 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, this can be cumbersome, because sometimes I have to select 40 columns. How can I get around this?

I even tried

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

but he also gave me errors.

I think this should be a simple thing, but I could not find a solution on the Internet.

+8
python pandas dataframe
source share
1 answer

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 this helps!

+10
source share

All Articles