How to do boolean slicing on array of tuples in numpy

Viewed 36

I have a .csv file like this

vehicle,speed,datetime,x,y
61C22276,0.0,1.4926212E9,106.33695,11.12652
60C28912,0.0,1.4926212E9,106.84327166666667,10.90424
51D06538,0.0,1.4926212E9,106.7806,10.765768333333334
50LD08650,0.0,1.4926212E9,106.91705,10.746173333333333
50LD08519,41.0,1.4926212E9,106.95493,10.739623333333334
50LD07182,0.0,1.4926212E9,106.917225,10.746073333333333

I imported this data into numpy by

my_data = genfromtxt('data/2017-04-20.csv',names=True,delimiter=',')

The output is:

[(b'61C22276',  0.,   1.49262120e+09,  106.33695   ,  11.12652   )
 (b'60C28912',  0.,   1.49262120e+09,  106.84327167,  10.90424   )
 (b'51D06538',  0.,   1.49262120e+09,  106.7806    ,  10.76576833) ...,
 (b'61C18919',  0.,   1.49265726e+09,  106.77865833,  11.03690667)
 (b'61C18919',  0.,   1.49265729e+09,  106.77865833,  11.03690667)
 (b'61C18919',  0.,   1.49265732e+09,  106.77865833,  11.036905  )]

which is an array of tuples (because my data consists of multiple types)

How do I slice my_data based on a value of a column? (Ex: List all the rows of the vehicle 61C2226)

1 Answers

You've got a structured array. Then to select a 'row', here is the way :

boolindex=my_data['vehicle']=='50LD08519'
selection=my_data[boolindex] 

#array([('50LD08519', 0.0, 1492621184.0, 106.91705322265625, 10.746172904968262),
#      ('50LD08519', 41.0, 1492621184.0, 106.9549331665039, 10.739623069763184)], 
#      dtype=[('vehicle', '<U'), ('speed', '<f4'), ('datetime', '<f4'), 
#      ('x', '<f4'), ('y', '<f4')])

'Pandas' give you more kindy I/O and intuitive syntax :

In [521]: my_data=pd.read_csv('data.csv')

         vehicle  speed      datetime   x  y
0   61C22276      0 1,492,621,200 106 11
1   60C28912      0 1,492,621,200 107 11
2   51D06538      0 1,492,621,200 107 11
3  50LD08519      0 1,492,621,200 107 11
4  50LD08519     41 1,492,621,200 107 11
5  50LD07182      0 1,492,621,200 107 11

In [522]: my_data[my_data['vehicle']=='50LD08519']
Out[522]: 
     vehicle  speed      datetime   x  y
3  50LD08519      0 1,492,621,200 107 11
4  50LD08519     41 1,492,621,200 107 11
Related