Is it possible in np.loadtxt to load all columns except the first one, or except some particular ones, please?
What does usecols = (0,) mean, please? Is it possible to use sliders?
I have 55 columns, and I would like to load all except one. Is there better way than writing usecols = (1, 2, 3, 4, 5, 6, 7)
and continue to 55?
CodePudding user response:
I don't think there's a way to exclude certain columns without specifying all columns.
Since you just want to exclude one column, you might as well read the entire thing and then slice it out with data = data[1:]
.
If you really don't want to do that, you can do usecols=range(1, 56)
instead of typing out all the numbers.
For a more general approach, you could write a function that takes the number of columns and a list of columns to exclude, and creates the usecols
argument automatically:
def loadtxt_excludecols(exclude_cols, num_cols, *args, **kwargs):
cols = set(range(num_cols))
cols -= set(exclude_cols)
cols = sorted(list(cols))
return np.loadtxt(*args, **kwargs, usecols=cols)
data = loadtxt_excluldecols([1, 10, 30], 50, 'filename.dat', ...other loadtxt args)