6
$\begingroup$

From some calculations I have ended with Datasets of the form:

{ds1, ds2} = Dataset /@ {<|"a" -> 1, "b" -> 2|>, <|10 -> 3, 20 -> 4|>}

enter image description here

I would like to combine and transpose these as you can do with lists (e.g. Transpose[{list1, list2}]). I tried Transpose[<|Normal@ds1, Normal@ds2|>] which doesn't work but the following does:

Apply[Association, Transpose[(Normal@# /. {Association -> List}) & /@ {ds1, ds2}], {1}]
(* {<|"a" -> 1, 10 -> 3|>, <|"b" -> 2, 20 -> 4|>} *)

However, these seems overly verbose for such a simple transpose. Is there a built-in function that I have missed? Or some terser (yet readable) combination of functions?

$\endgroup$
  • 1
    $\begingroup$ How about MapThread[Association, Normal@*Normal /@ {ds1, ds2}]? $\endgroup$ – Leonid Shifrin May 27 '16 at 12:56
  • $\begingroup$ @LeonidShifrin Very nice! (+1) I knew I was missing something. $\endgroup$ – Edmund May 27 '16 at 13:09
  • $\begingroup$ Ok, I decided to make it an answer. $\endgroup$ – Leonid Shifrin May 27 '16 at 13:20
5
$\begingroup$

Not sure about a built-in, but you can do

MapThread[Association, Normal@*Normal /@ {ds1, ds2}]
{<|"a" -> 1, 10 -> 3|>, <|"b" -> 2, 20 -> 4|>} 
| improve this answer | |
$\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.