Reputation: 1311
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?
Upvotes: 1
Views: 419
Reputation: 58865
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.]]
Upvotes: 1