1

I want to load only last few columns in a text file with some evaluation.

I used numpy.genfromtxt with the argument converters={-1:func,-2:func}

But it is not working. On the other hand if i give the forward indexing like converters={56:func,57:func} it works correctly.

Why doesn't converters argument support the python's backward indexing? Is there anyway to do this if i know only the indexing of column from the last?

indiajoe
  • 1,291
  • 3
  • 13
  • 26

1 Answers1

1

Using numpy.loadtxt it works, and you can use the converters parameter to define your functions. Having a tmp.txt file with:

11,12,13,14,15,16,17,18,19
21,22,23,24,25,26,27,28,29
31,32,33,34,35,36,37,38,39
41,42,43,44,45,46,47,48,49
51,52,53,54,55,56,57,58,59

You can load the selected columns with (also chosing the order which you want them to be stacked):

import numpy as np
print np.loadtxt('tmp.txt',delimiter=',',usecols=(-2,-1))
#[[ 18.  19.]
# [ 28.  29.]
# [ 38.  39.]
# [ 48.  49.]
# [ 58.  59.]]
print np.loadtxt('tmp.txt',delimiter=',',usecols=(-1,-2),converters={-1: lambda x: float(x)+100})
#[[ 119.   18.]
# [ 129.   28.]
# [ 139.   38.]
# [ 149.   48.]
# [ 159.   58.]]
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
  • Yes, np.loadtxt supports backward indexing. but my text file was not fully numbers, some columns have string entries. so i couldn't use loadtxt. – indiajoe Jul 05 '13 at 19:18
  • 1
    you can, just pass `dtype=object` or, if you want to read each column using a different type in a structured array: `dtype={'names': ('col1', 'col2', 'col3'), 'formats': (int, str, float)}`, for example – Saullo G. P. Castro Jul 05 '13 at 19:21
  • Oh. ok thanks. with this feature, i should be able to manage with loadtxt. I think, as long as i don't have a missing value entry in table, i should be able to use loadtxt instead of genfromtxt. Thanks again. – indiajoe Jul 05 '13 at 19:27