Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy loadtxt skip first column

Tags:

python

numpy

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!

like image 701
user1314776 Avatar asked Dec 17 '13 01:12

user1314776


People also ask

What does Loadtxt () do in NumPy?

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.


1 Answers

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'))
like image 150
mgilson Avatar answered Sep 28 '22 11:09

mgilson