问题
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