I have a text file made as:
0.01 1 0.1 1 10 100 a
0.02 3 0.2 2 20 200 b
0.03 2 0.3 3 30 300 c
0.04 1 0.4 4 40 400 d
I read it as a list A
and then converted to a numpy array, that is:
>>> A
array([['0.01', '1', '0.1', '1', '10', '100', 'a'],
['0.02', '3', '0.2', '2', '20', '200', 'b'],
['0.03', '2', '0.3', '3', '30', '300', 'c'],
['0.04', '1', '0.4', '4', '40', '400', 'd']],
dtype='|S4')
I just want to extract a sub-array B
, made of A
wherever its 4th entry is lower than 30, that should look something like:
B = array([['0.01', '1', '0.1', '1', '10', '100', 'a'],
['0.02', '3', '0.2', '2', '20', '200', 'b']])
When dealing with arrays, I usually do simply B = A[A[:,4]<30]
, but in this case (maybe due to the presence of characters/strings I've never worked with) it doesn't work, giving me this:
>>> A[A[:,4]<30]
array(['0.01', '1', '0.1', '1', '10', '100', 'a'],
dtype='|S4')
and I can't figure out the reason. I'm not dealing with a code of mine and I don't think I can switch all this to structures or dictionaries: any suggestion for doing this with numpy arrays? Thank you very much in advance!