user2015601
user2015601

Reputation:

Reading just 1 column from a file using NumPy's loadtxt() function

I want to read in data from multiple files that I want to use for plotting (matplotlib).

I found a function loadtxt() that I could use for this purpose. However, I only want to read in one column from each file.

How would I do this? The following command works for me if I read in at least 2 columns, for example:

numpy.loadtxt('myfile.dat', usecols=(2,3))

But

numpy.loadtxt('myfile.dat', usecols=(3))

would throw an error.

Upvotes: 15

Views: 18312

Answers (1)

unutbu
unutbu

Reputation: 879491

You need a comma after the 3 in order to tell Python that (3,) is a tuple. Python interprets (3) to be the same value as the int 3, and loadtxt wants a sequence-type argument for usecols.

numpy.loadtxt('myfile.dat', usecols=(3,))

Upvotes: 22

Related Questions