I am trying to load some data stored in a CSV
file where the headers are in the first column. I am using numpy.loadtxt
(v1.6).
I was wondering if there is a way to load all the columns except the first?
I know that it is possible to choose the columns we want to load. The thing is, I don't know how many columns (I just know that there are more than 255 columns).
If someone has trick to do that, it would be great!
loadtxt() function. The loadtxt() function is used to load data from a text file. Each row in the text file must have the same number of values.
Well, It looks like you can feed it a generator, so just strip the first column in a generator:
def strip_first_col(fname, delimiter=None):
with open(fname, 'r') as fin:
for line in fin:
try:
yield line.split(delimiter, 1)[1]
except IndexError:
continue
data = np.loadtxt(strip_first_col('myfilename'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With