I feel like this is a simple task, and there may be a built in method to doing this (but I can't find it if there is). I would like to be able to take a table and convert it into a structured Dataset. Preferably, I'd like it to be generic for any number of columns and rows. Lets assume that I have a table Tbl
and a list of column names Col
and that Tbl
and Col
have the same number of columns.
Col={"ColA","ColB","ColC"};
Tbl={{1,2,3},{4,5,6},{7,8,9}};
For Interest, I might also like it to handle an optional list of row names RowNames
.
RowNames={"Row1","Row2","Row3"};
Please let me know if you can come up with a simple way to do this
The final desired format would be something like this:
Result = Dataset[{<|"ColA" -> 1, "ColB" -> 2, "ColC" -> 3|>,
<|"ColA" -> 4, "ColB" -> 5, "ColC" -> 6|>,
<|"ColA" -> 7, "ColB" -> 8, "ColC" -> 9|>}]
or this if it used RowNames:
Dataset[<|"Row1" -> <|"ColA" -> 1, "ColB" -> 2, "ColC" -> 3|>,
"Row2" -> <|"ColA" -> 4, "ColB" -> 5, "ColC" -> 6|>,
"Row3" -> <|"ColA" -> 7, "ColB" -> 8, "ColC" -> 9|>|>]
Row
is a protected built-in symbol in Mathematica, you can't use it as a variable. Please write down the output you are trying to achieve, i.e. what the finalAssociation
should look like. $\endgroup$Dataset@AssociationThread[row, AssociationThread[col, #] & /@ tbl]
... I'm waiting to see better ways as I find this too long. $\endgroup$Dataset[tbl][All, AssociationThread[col, Range[Length[col]]]]
$\endgroup$