2
$\begingroup$

I am struggling with extracting specific rows from a dataset.

dataset = 
 Dataset[{<|"a" -> 1, "b" -> "x", "c" -> {1}|>, <|"a" -> 2, 
    "b" -> "y", "c" -> {2, 3}|>, <|"a" -> 3, "b" -> "z", 
    "c" -> {3}|>, <|"a" -> 4, "b" -> "x", "c" -> {4, 5}|>, <|"a" -> 5,
     "b" -> "y", "c" -> {5, 6, 7}|>, <|"a" -> 6, "b" -> "z", 
    "c" -> {}|>}]

enter image description here

I would like to get a new dataset consisting only of examples that have a specific number in column "a" (for example {2,4,5})

I can retrieve one specific row by using the following: Select[dataset, #a == 2 &] : enter image description here

But I don't know how to do that for a list with the given numbers. I tried the following which gave me an empty dataset:

Select[dataset, #a == {2, 4, 5} &]

enter image description here

Thank you very much for your help.

$\endgroup$
6
$\begingroup$
Query[Select[MemberQ[{2, 4, 5}, #a] &]]@dataset

enter image description here

$\endgroup$
5
$\begingroup$

You can also use

dataset[Select[MatchQ[#a, 2 | 4 | 5] &]]

enter image description here

Select[MatchQ[#a, 2 | 4 | 5] &] @ dataset

enter image description here

Cases[KeyValuePattern["a" -> 2 | 4 | 5]]@dataset

enter image description here

$\endgroup$
2
$\begingroup$

The above answers are excellent, and complete, but when prototyping code, I often find the array indexing with datasets to be useful and sometimes a bit easier to type. Like so:

dataset[[{2,4,5}]]

yields your desired rows 2,4,5

Also:

Position[dataset[[;;,1]],2|4|5]

extracts the row positions which meet your criteria above,

And:

dataset[[Flatten@Position[dataset[[;;,1]],2|4|5]]]

accomplishes the same as the above answers, in a way which somehow makes more sense to me.

Just my 0.02 - I should mention I have no idea how this handles column indexing for datasets, which is probably quite important to you. My data tends to be very regular, so indexing by column number is usually sufficient.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.