[英]How to deselect row(s) at specific indices in pydatatable?
I have a datatable as below,我有一个如下的数据表,
| season title rating
-- + ------ --------------------- ------
0 | 10 The last one 9.7
1 | 1 The pilot 5.6
2 | 4 The one where estelle 7.8
3 | 10 The last one 9.7
4 | 3 The thumb 10
[5 rows x 3 columns]
Here row 0 and 3 are duplicated, I would like to keep the 3rd row a side.这里第 0 行和第 3 行重复,我想将第 3 行保留在一边。
In this example, i tried it as -在这个例子中,我试了一下 -
DT_X[f.season!=10, :]
It filters the both 2 observations as below,它过滤了两个观察结果,如下所示,
| season title rating
-- + ------ --------------------- ------
0 | 1 The pilot 5.6
1 | 4 The one where estelle 7.8
2 | 3 The thumb 10
[3 rows x 3 columns]
But, i would like to have a first observation which is having 0 index and filtering out the 3rd row.但是,我想要第一个观察结果,它具有 0 索引并过滤掉第 3 行。
How can it be achieved in pydatatable ?如何在 pydatatable 中实现? how to retrieve datatable rows using their indices ?.
如何使用索引检索数据表行?
排除第 3 个索引:
DT[[slice(2), 3], :]
在与 H2O 的团队和 @sammyweemmy 讨论之后,我找到了一个使用函数切片的解决方案,如下所示。
DT_X[[slice(3),slice(4,None)],:]
The easiest way to "deselect" rows is to simply delete them from the frame: “取消选择”行的最简单方法是简单地从框架中删除它们:
del DT_X[3, :] # remove the row at index 3
If you don't want to modify the original frame, then simply create a copy first (it's fast because the data is not copied):如果不想修改原始帧,那么只需先创建一个副本(速度很快,因为没有复制数据):
DT_clean = DT_X.copy()
del DT_clean[3, :]
This is particularly convenient if you need to remove several rows: just pass a list of indices as the first argument.如果您需要删除多行,这特别方便:只需将索引列表作为第一个参数传递。
The solution with slices works too: it is based on the fact that i
selector can have a list of things in it (see docs ).切片的解决方案也有效:它基于
i
选择器可以在其中包含内容列表的事实(请参阅文档)。 Thus, in order to "deselect" row 3, we would select a slice :3
and then another slice 4:
.因此,为了“取消选择”第 3 行,我们将选择一个切片
:3
,然后选择另一个切片4:
。 Unfortunately, Python allows slice notation only inside square brackets, thus we have to specify the above slices explicitly: slice(None, 3)
and slice(4, None)
.不幸的是,Python 只允许在方括号内使用切片符号,因此我们必须明确指定上述切片:
slice(None, 3)
和slice(4, None)
。 Putting them together results in the solution posted by @myamulla_ciencia:将它们放在一起会产生@myamulla_ciencia 发布的解决方案:
DT_X[[slice(None, 3), slice(4, None)], :]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.