Skip to content Skip to sidebar Skip to footer

Skip A Specified Number Of Columns With Numpy.genfromtxt()

I have a large table (numbers in text format) that I would like to load with numpy.genfromtxt(). I would like to ignore the first n columns, say 5. I do not know the size of the ta

Solution 1:

For older versions of numpy, peeking at the first line to discover the number of columns is not that hard:

import numpy as np
with open(fname, 'r') as f:
    num_cols = len(f.readline().split())
    f.seek(0)
    data = np.genfromtxt(f, usecols = range(5,num_cols))
print(data)

Solution 2:

In newer versions of Numpy, np.genfromtxt can take an iterable argument, so you can wrap the file you're reading in a generator that generates lines, skipping the first N columns. If your numbers are space-separated, that's something like

np.genfromtxt(" ".join(ln.split()[N:]) for ln in f)

Post a Comment for "Skip A Specified Number Of Columns With Numpy.genfromtxt()"