Skip to content
Advertisement

Numpy import txt file as matrix without text lines

I have a txt file like the following, where I want to import the last column without the text so that I can do matrix vector operations with it

JavaScript

I tried numpy.loadtxt("path")[1:,4] but get the following error which I assume is due to the text inside my txt file

JavaScript

Advertisement

Answer

Numpy loadtxt has a number of parameters:

  • skiprows allows to skip the initial rows
  • usecols allows to only load a selection of columns

Here what you want is:

JavaScript

With the sample data, it gives as expected:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement