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$
7
$\begingroup$
dataset[Select[StringMatchQ[#a, "apple*"] &]]

enter image description here

Also

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

same result

$\endgroup$
  • $\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 Dec 16 '19 at 3:24
  • $\begingroup$ maybe dataset[Cases[KeyValuePattern[_ -> _String?(StringMatchQ["apple*"])]]] or dataset[Select[ Not@*FreeQ[KeyValuePattern[_ -> _String?(StringMatchQ["apple*"])]]]]? $\endgroup$ – kglr Dec 16 '19 at 4:20
  • 1
    $\begingroup$ ... also dataset[Select[ MatchQ[KeyValuePattern[_ -> _String?(StringMatchQ["apple*"])]]]] $\endgroup$ – kglr Dec 16 '19 at 4:26

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.