简体   繁体   English

如何根据 pydatatable 中指定的索引进行 select 观察?

[英]How to select observations based on Index specified in pydatatable?

I have a datatable as -我有一个数据表 -

DT = dt.Frame(
     A=[1, 3, 2, 1, 4, 2, 1], 
     B=['A','B','C','A','D','B','A'],
     C=['myamulla','skumar','cary','myamulla','api','skumar','myamulla'])
Out[14]: 
   |  A  B   C       
-- + --  --  --------
 0 |  1  A   myamulla
 1 |  3  B   skumar  
 2 |  2  C   cary    
 3 |  1  A   myamulla
 4 |  4  D   api     
 5 |  2  B   skumar  
 6 |  1  A   myamulla

[7 rows x 3 columns]

I'm now going to select an observation which has api in column C as -我现在要去 select 观察,其中 api 在 C 列中为 -

DT[f.C=="api",:]
Out[12]: 
   |  A  B   C  
-- + --  --  ---
 0 |  4  D   api

OK,Now i would like to find an index related to this observation so that I can select the observation from this index onwards in datatable,好的,现在我想找到一个与这个观察相关的索引,这样我就可以在数据表中从这个索引开始观察 select,

For example the above observation has got a row number 4 in DT, I can select the observations from 4th onwards as -例如,上面的观察在 DT 中有第 4 行,我可以 select 从第 4 开始观察 -

DT[4:,:]
Out[15]: 
   |  A  B   C       
-- + --  --  --------
 0 |  4  D   api     
 1 |  2  B   skumar  
 2 |  1  A   myamulla

But what if have millions of observations in DT, i can't figure out the required observation index.但是,如果在 DT 中有数百万个观察值,我无法计算出所需的观察指数。

One way around it is to create a temporary index column:一种解决方法是创建一个临时索引列:

from datatable import dt, f, update
DT[:, update(index = range(DT.nrows))]

In [8]: DT
Out[8]: 
   |     A  B      C         index
   | int32  str32  str32     int32
-- + -----  -----  --------  -----
 0 |     1  A      myamulla      0
 1 |     3  B      skumar        1
 2 |     2  C      cary          2
 3 |     1  A      myamulla      3
 4 |     4  D      api           4
 5 |     2  B      skumar        5
 6 |     1  A      myamulla      6
[7 rows x 4 columns]

Now create a filter in i to select the index downwards:现在在i中创建一个过滤器到 select 索引向下:

In [11]: DT[DT[f.C=='api', 'index'][0,0]:, :-1]
Out[11]: 
   |     A  B      C       
   | int32  str32  str32   
-- + -----  -----  --------
 0 |     4  D      api     
 1 |     2  B      skumar  
 2 |     1  A      myamulla
[3 rows x 3 columns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM