So I know this should be pretty easy, but I cannot seem to find a solution anywhere, so thanks in advance for your help: I have three Lists (with numbers as elements) named lista, listb and listc and I want to construct adataset with columns "a" to "c", where column "a" contains all elements from "lista" and so on. I then want to be able to use the data in other dataset operations (like Select or LinearModelFit etc.). And, while I don't know if it makes a difference, each list has 1000 elements and all lists are therefore equally long. Thanks again for inputs!

  • $\begingroup$ Welcome to MSE! Assuming your lists are the same length you can do data=Transpose[{lista,list,listc}] and the proceed as you desire. Try things for yourself. If you get into trouble then you can seek help. Ideally, a minimal working example makes it easier for responders (and may even provide you your own insight). :) $\endgroup$
    – ubpdqn
    Apr 4 '16 at 9:49
  • $\begingroup$ ResourceFunction["DatasetWithHeaders"] $\endgroup$
    – Searke
    Nov 19 '19 at 18:01

Let's take some example lists:

lis1 = {1, 2, 3, 4, 5};
lis2 = {6, 7, 8, 9, 0};
lis3 = {2, 4, 6, 8, 9};

Now you can combine your lists into one list and your headers into a different list:

data = {lis1, lis2, lis3};
header = {"a", "b", "c"};

To create a dataset just do:

Dataset@Map[AssociationThread[header, #] &]@Transpose[data]

Mathematica graphics

  • $\begingroup$ I didn't know about AssociationThread, was trying to make it up from scratch like Association @@@ Transpose[ {Thread["a" -> lista], Thread["b" -> listb], Thread["c" -> listc]}] This is better $\endgroup$
    – Jason B.
    Apr 4 '16 at 9:55
  • 2
    $\begingroup$ @JasonB, Yeah, it's a nice little function that makes life easier :) $\endgroup$
    – RunnyKine
    Apr 4 '16 at 9:57
  • $\begingroup$ +1).Rarely to use Map like this. $\endgroup$
    – yode
    Apr 4 '16 at 15:31
  • $\begingroup$ @yode, Thanks. Yeah, the operator form is nice sometimes. $\endgroup$
    – RunnyKine
    Apr 4 '16 at 21:56

If using Mathematica version 10.1 or later, we can write:

lista = {1, 2, 3, 4, 5};
listb = {6, 7, 8, 9, 0};
listc = {2, 4, 6, 8, 9};

<| "a" -> lista, "b" -> listb, "c" -> listc |> // Query[Transpose] // Dataset

Dataset screenshot

We can also write:

<| "a" -> lista, "b" -> listb, "c" -> listc |> // Dataset // Transpose

... but version 10.4 introduced a regression where the resulting dataset loses its type information (the expression works fine in versions 10.1, 10.2 and 10.3).

  • 1
    $\begingroup$ +1. I was waiting for you to chime in :). Yeah, I realized the Dataset functionality goes through changes all the time with no mention of these changes in the documentation. $\endgroup$
    – RunnyKine
    Apr 4 '16 at 21:53
  • $\begingroup$ makes it hard to write and reuse code when Dataset is such a moving target $\endgroup$ Apr 5 '16 at 0:13
  • $\begingroup$ @MikeHoneychurch Yes, I tend to avoid Dataset myself. Its main purpose is to visualize data and I find its visualizations to be problematic maybe 50% of the time. It seems to be overly sensitive to the inferred data types, especially when associations are involved. And a moving target, as you say. Query, on the other hand, I have grown to like. It is more stable than Dataset and I find the "generalized index notation" to be very expressive. $\endgroup$
    – WReach
    Apr 5 '16 at 13:05
  • $\begingroup$ @WReach thanks for your 2 cents. I'll spend some more time with Query $\endgroup$ Apr 5 '16 at 22:48

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.