How to deselect row(s) at specific indices in pydatatable?

Viewed 107

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.

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.

How can it be achieved in pydatatable ? how to retrieve datatable rows using their indices ?.

3 Answers

Exclude the 3rd index:

DT[[slice(2), 3], :]

after discussing it on H2O's team and @sammyweemmy i have figured out a solution using a function slice as below.

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). Thus, in order to "deselect" row 3, we would select a slice :3 and then another slice 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). Putting them together results in the solution posted by @myamulla_ciencia:

DT_X[[slice(None, 3), slice(4, None)], :]
Related