# How to filter a dataset using Select and a parameter

I'd like to filter a Dataset using a parameter in Select, but so far I haven't been able to make it work properly. Say that I have the following data:

data = {
{"Group", "aa", "bb"}
, {"A", 3.24, 9.68}
, {"B", 3.5, "-"}
, {"C", "", 6.79}
};


I convert my data to a dataset:

dataset =


Extracting only these datasets, in which the aa data are numbers:

dataset[Select[NumberQ[#"aa"] &]]


Now I want to either select aa or bb using a parameter, but why doesn't this work?

sel = "aa"
dataset[Select[NumberQ[#sel] &]]

• Possible dupe. This is covered here: mathematica.stackexchange.com/questions/59732/… – Gordon Coale Jan 26 '15 at 14:03
• basically Select[#State =="New York" || #Population > 2000000 &]@cityDS (or cityAssoc) for a Dataset you can also use DS[Select[#State=="New York" &],"Population"] selecting the "Population" column of the dataset DS where the "State" column = New York. Use || for OR and && for AND logical operators – Gordon Coale Jan 26 '15 at 14:11
• dataset[Select[NumberQ[#[sel]] &]] or dataset[Select[NumberQ[#@sel] &]] . Read the scope section of the #(Slot) docs – SquareOne Jan 26 '15 at 14:19
• @SquareOne Thanks (again!). I did read through the #-Sektion, but couldn't get it to work. I'll certainly go through the Scope section again. – My program is finished now. Dataset is somewhat tricky to get your (i.e. my ...) head around. But it seems quite powerful once you get it. – Mockup Dungeon Jan 26 '15 at 20:21
• @Gordon Coale, I went through the topic you identified as possible dupe, but didn't find the answer there. Thanks for your input! – Mockup Dungeon Jan 26 '15 at 20:22

The example does not work because #sel is a special shorthand notation that is interpreted as if we had written #["sel"]. Observe that sel becomes quoted as part of the expansion, meaning that the expression will attempt to select the (non-existent) value associated with the literal string "sel". This shorthand is mentioned in the Details section of the documentation for Slot.

To make the example work, we need to explicitly specify sel in unquoted form so that it will be evaluated. #[sel] is what we need:

sel = "aa";
dataset[Select[NumberQ[#[sel]] &]]


Alternatively, we could use the part notation #[[sel]]:

dataset[Select[NumberQ[#[[sel]]] &]]


Such notation is listed in the documentation for Part.

Update

As noted by commentators, there is nothing special about the expression #[sel] that would prevent us from using prefix form (# @ sel) or postfix form (sel // #) if desired.

• worth adding squareones second example as a third way or doing it? – Gordon Coale Jan 27 '15 at 9:10
• @GordonCoale Done. – WReach Jan 27 '15 at 15:00