4

I'd like to create a Dataset from an array, where each column's heading becomes the key for all elements in that column. I can accomplish this using nested Table commands, but was wondering if there is a more elegant way that directly leverages the syntax developed for Datasets. E.g., could this instead be accomplished using GroupBy?:

list = {{"date", "time", "volume"}, {a1, a2, a3}, {b1, b2, b3}, {c1, 
   c2, c3}}
Table[<|Table[
   list[[1, i]] -> list[[n, i]], {i, 1, Length@list[[1]]}]|>, 
   {n, 2, Length@list}]
Dataset@%

enter image description here

theorist
  • 3,633
  • 1
  • 15
  • 27
  • 1
    I think you want something like AssociationThread[{"date", "time", "volume"} -> {{a1, a2, a2}, {b1, b2, b3}, {c1, c2, c3}}] – Carl Lange May 16 '21 at 10:29
  • @CarlLange That doesn't quite work--try applying Dataset to your code. But thanks for the suggestion to look at AssociationThread--I'll trying playing with it tomrrow. – theorist May 16 '21 at 10:35
  • Ah, it might be Dataset@AssociationThread[{"date", "time", "volume"} -> {{{a1, a2, a2}}, {{b1, b2, b3}}, {{c1, c2, c3}}}]? Sorry, I'm away from Mathematica at the minute :) (Related: https://mathematica.stackexchange.com/questions/246088/dataset-from-association-of-lists-doesnt-work) – Carl Lange May 16 '21 at 12:03
  • @CarlLange That works, but only after an extra level is added to Rest@list, restructuring it from{{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}} to {{{a1, a2, a3}}, {{b1, b2, b3}}, {{c1, c2, c3}}}. I could do that, but then I'm back to using Table. I tried Partition, but it only adds an extra set of braces on the outside: Partition[Rest@list, 3]=>{{{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}}} – theorist May 16 '21 at 16:36

2 Answers2

5

This common data format, where the first row of data has column names, and the following rows are values, is easy to convert to a dataset with AssociationThread.

list = {{"date", "time", "volume"},
    {a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}};

ds = Dataset[AssociationThread[First@list, #] & /@ Rest@list]

Often, data from comma-separated values and other tabular file formats is arranged in the same way. For example, it's easy to import a CSV file to a dataset with the HeaderLines option. This avoids the need to convert an imported array.

ds = Import["file.csv", "Dataset", HeaderLines -> 1]

Related: How to display properly Dataset with index column?

creidhne
  • 5,055
  • 4
  • 20
  • 28
0

a little bit late but:

Dataset[AssociationThread[list[[1]]->Transpose@list[[2 ;;]]]]//Transpose
Robert Nowak
  • 861
  • 6
  • 12