pandas read_table usecols 错误为“:”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35400861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:41:51  来源:igfitidea点击:

pandas read_table usecols error with ":"

pythonpandasdataframe

提问by BioProgram

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:

我正在尝试使用 python pandas read_table 函数从我的文件中读取一定范围的非连续列。为此,我正在尝试:

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.

这个想法是我试图使用“:”来选择 usecols 的列数范围,而不是使用以逗号“,”分隔的列号。我收到语法错误。如果我使用逗号“,”来分隔列号,那么它工作正常。

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?

但是,这可能很麻烦,因为有时我必须选择 40 列。我怎样才能解决这个问题?

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.

我认为这应该是解决的简单问题,但我无法在网上找到解决方案。

回答by cdonts

It should be...

它应该是...

Python 3:

蟒蛇3:

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

Python 2:

蟒蛇2:

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

Hope it helps!

希望能帮助到你!