Applying functions to some columns in a Dataset and keeping only those

Take the following example Dataset:

data = Table[Association["a" -> i, "b" -> i^2, "c" -> i^3], {i, 4}] // Dataset

PZSgO Applying functions to some columns in a Dataset and keeping only those

Picking out two of the three columns is done this way:

data[All, {"a", "b"}]

LG3jU Applying functions to some columns in a Dataset and keeping only those

Now instead of just returning the “a” and “b” columns I want to map the functions f and h to their elements, respectively, and still drop “c”. Based on the previous result and the documentation of Dataset I hoped the following would do that:

data[All, {"a" -> f, "b" -> h}]

Wvwml Applying functions to some columns in a Dataset and keeping only those

As you can see, the behavior is not like the one before. Although the functions are mapped as desired, the unmentioned column “c” still remains in the data.

Do I really need one of the following (clumsy looking) alternatives

data[All, {"a" -> f, "b" -> h}][All, {"a", "b"}]
data[Query[All, {"a", "b"}], {"a" -> f, "b" -> h}]
Query[All, {"a", "b"}]@data[All, {"a" -> f, "b" -> h}]

to get:

q62un Applying functions to some columns in a Dataset and keeping only those

or is there a more elegant solution?

Let’s block ads! (Why?)

Recent Questions – Mathematica Stack Exchange