Selecting specific range of columns from .CSV file [duplicate]

大兔子大兔子 提交于 2019-12-22 18:06:05

问题


I have a CSV file which has 78000 columns. I am trying to select the columns 2-100, 102-200, and the last 300 columns. The rest of the columns need to be skipped.

I have used numpy.loadtxt to select range of columns:

numpy.loadtxt(input_file_name, delimiter=",", skiprows = 1, usecols=range(1,99))

How can we select blocks of columns doing something similar, like:

numpy.loadtxt(input_file_name, delimiter=",", skiprows = 1, usecols=(range(1,99),range(101,199),range(74999,77999)))

回答1:


Use the numpy row selector, np.r_.

>>> np.r_[range(3), range(15, 18), range(100, 103)]

Or (using hpaulj's suggestion),

>>> np.r_[0:3, 15:16, 100:103]

array([  0,   1,   2,  15,  16,  17, 100, 101, 102])

For your code, this is how you'd call it -

numpy.loadtxt(
  input_file_name, 
  delimiter=",", 
  skiprows = 1, 
  usecols=np.r_[range(1, 99), range(101, 199), range(74999, 77999)]
)


来源:https://stackoverflow.com/questions/48337371/selecting-specific-range-of-columns-from-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!