4
$\begingroup$

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

$\endgroup$
4
  • 1
    $\begingroup$ I think you want something like AssociationThread[{"date", "time", "volume"} -> {{a1, a2, a2}, {b1, b2, b3}, {c1, c2, c3}}] $\endgroup$ Commented May 16, 2021 at 10:29
  • $\begingroup$ @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. $\endgroup$ Commented May 16, 2021 at 10:35
  • $\begingroup$ 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: mathematica.stackexchange.com/questions/246088/…) $\endgroup$ Commented May 16, 2021 at 12:03
  • $\begingroup$ @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}}} $\endgroup$ Commented May 16, 2021 at 16:36

2 Answers 2

5
$\begingroup$

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?

$\endgroup$
0
$\begingroup$

a little bit late but:

Dataset[AssociationThread[list[[1]]->Transpose@list[[2 ;;]]]]//Transpose 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.