What is Numpy equivalence of dataframe.loc() in Pandas

  • A+
Category:Languages

I have a 120,000*4 numpy array as shown below. Each row is a sample. The first column is time in second, or the index using Pandas terminology.

0.014      14.175  -29.97  -22.68  0.022      13.905  -29.835 -22.68 0.030      12.257  -29.32  -22.67 ... ... 1259.980   -0.405   2.205   3.825 1259.991   -0.495   2.115   3.735 

I want to select the rows recorded between 100.000 to 200.000 sec and save it into a new array. If this were a Pandas dataframe, I would simply write df.loc[100:200]. What is the equivalent operation in numpy?

This is NOT a question of feasibility. I simply wonder if there are any pythonic one-line solutions.

 


This assumes indexes are sorted:

IIUC,

x=np.array([ [1,2,3,4],            [5,6,7,8],            [9,10,11,12],            [13,14,15,16]])  x[(x[:,0] >= 5) & (x[:,0] <= 9) ] 

So you would have 100 and 200 instead of 5 and 9.


For a more general solution, check Wen`s answer

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: