3
$\begingroup$

For dataset

dataset = Dataset[{
   <|"a" -> "apple1", "b" -> "x", "c" -> {1}|>,
   <|"a" -> "apple2", "b" -> "y", "c" -> {2, 3}|>,
   <|"a" -> "banana", "b" -> "z", "c" -> {3}|>}]

How does one filter/narrow this dataset down to entries whose "a" field matches some string pattern, for example the string pattern "apple*"?

$\endgroup$

1 Answer 1

7
$\begingroup$
dataset[Select[StringMatchQ[#a, "apple*"] &]]

enter image description here

Also

Query[Select[StringMatchQ[#a, "apple*"] &]] @ dataset

same result

$\endgroup$
3
  • $\begingroup$ Is there a way to make this search -- not against just #a -- but against all fields in an (arbitrary) Dataset? (Similar to piping a string through grep in a shell console)? $\endgroup$
    – George
    Commented Dec 16, 2019 at 3:24
  • $\begingroup$ maybe dataset[Cases[KeyValuePattern[_ -> _String?(StringMatchQ["apple*"])]]] or dataset[Select[ Not@*FreeQ[KeyValuePattern[_ -> _String?(StringMatchQ["apple*"])]]]]? $\endgroup$
    – kglr
    Commented Dec 16, 2019 at 4:20
  • 1
    $\begingroup$ ... also dataset[Select[ MatchQ[KeyValuePattern[_ -> _String?(StringMatchQ["apple*"])]]]] $\endgroup$
    – kglr
    Commented Dec 16, 2019 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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