Search code examples
python-3.xnumpygenfromtxt

Read specific columns of .txt file with numy genfromtxt


I try to read a .txt file containing 4 columns with names col1, col2, col3 and col4, with data types string, string, float and float.

I just want to read the columns col3 and col4 (for this example).

I used: table = numpy.genfromtxt(filename, skip_header=1, usecols=(2, 3))

Then I figured out that the strings in columns col1 and col2 could be phrases (separated by spaces), then usecols is taking the wrong columns.

Is it possible to get the last n columns ?

Is it possible to read the columns by using specific column name ?

Thanks


Solution

  • usecols parameter accepts sequence of negative numbers.

    To get last two columns :

    table = numpy.genfromtxt(filename, skip_header=1, usecols=(-2, -1))