Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Numpy loadtxt in python – except some colums

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading