# Remove rows from dataset using query

MyDataSet[Select[#point[Fsr] == 1 &]] returns the row I want to delete, now I am trying what looks to me the most logic command to remove these rows, which is MyDataSet[Delete[#point[Fsr] == 1 &]] MyDataSet[DeleteCases[#point[Fsr] == 1 &]], but neither works .... any idea how this should be done?

• What does MyDataSet[Select[#point[Fsr] != 1 &]] do? Oct 26 '15 at 23:25
• ... undeniably it gives me a new dataset without the rows I wanted to remove, but I have two concerns about this method: 1) for a large dataset, isn't it better to pullout what you do not need (a few items) rather than recreating a large dataset? 2) I wonder if there are cases where reversing the condition in Select does not do the job... Oct 26 '15 at 23:36
• Well, an expression test is an expression test, negated or not. It will have to go through each row to return its results. Oct 26 '15 at 23:38
• I do not have a large thing to test this on, but sounds like one of these Mathematica performance things, like Table performs very differently from Map ... Oct 26 '15 at 23:43

Here's one way, not necessarily the most efficient:

MyDataSet[Select[#point[Fsr] == 1 & /* Not]]

• That's just composing Not[] and the equality test, no? Why not use Unequal[] to begin with? Oct 28 '15 at 18:17
• @J.M., correct, the advantange of this composition over Unequal is it turns Select into delete independent of the matching logic, eg if the user replaced == by <. Oct 28 '15 at 18:36

There is a workaround if you use the named row style of Dataset (an association of associations) but don't actually apply Dataset to it. You can still perform querying as you can with a dataset but will have to explicitly use Query instead of it being implicitly used in the dataset[...] format.

assco = Association @@
MapIndexed[First@#2 -> #1 &]@
{<|"a" -> 5, "b" -> 2|>, <|"a" -> 3, "b" -> 2|>, <|"a" -> 5, "b" -> -1|>};


Here I have just indexed them with row number but you can use any expression. Now KeyDropFrom can be used with Select to drop the rows you are not interested in without making a new copy of the data. Below the rows with "a"-> 5 are dropped; the first and last row.

KeyDropFrom[assco, Keys@Select[#a == 5 &][assco]];
assco
(* <|2 -> <|"a" -> 3, "b" -> 2|>|> *)


The second row remains in assco with the others dropped.

Hope this helps.