3
$\begingroup$

Dataset from doc:

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

There is a built-in way to Divide all column by constant, i.e. 2:

dataset[Divide[#, 2] &, "a"]

If I want to Divide all values of "a" to corresponding values of "b" the naive way is as follows:

Normal[dataset[All, "a"]] / Normal[dataset[All, "b"]]

What are more natural ways to make such calculations, say, Division, between columns in Dataset?

$\endgroup$
  • 1
    $\begingroup$ Normal[Divide@@@dataset] or Normal[Query[All, Divide@@#&]@dataset]? $\endgroup$ – kglr May 8 at 7:45
3
$\begingroup$
Normal @ dataset[All, #a / #b&]

{1/("x"), 2/("y"), 3/("z"), 4/("x"), 5/("y"), 6/("z")}

Also:

Normal @ dataset[All, Apply[Divide]]
Normal[Divide @@@ dataset] 
Normal[Query[All, Divide @@ # &]@dataset]
$\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.