Skip to main content
5 of 5
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/

Create Nested List from tabular data

Im trying to create a nested list from tabular data, then onward into a nested association. I can get part way with the help of this answer but it only works for the first level. Repeated use of this approach doesn't seem to work.

list= {{"Category", "Detail", "Value"}, {"Cat1", "detail1", 23}, {"Cat1", "detail2", 27}, {"Cat1", "detail3", 13}, {"Cat2", "detail4",15}, {"Cat2", "detail5", 35}, {"Cat3", "Detail6", 56}}; list // TableForm (* tabular data result *) 

First level of data.

Thread[list[[2 ;;, 2]] -> #] & /@ {list[[2 ;;, 3]]} // ToAssociations {<|detail1->23,detail2->27,detail3->13,detail4->15,detail5->35,Detail6->56|>} 

Required result

{<| Cat1-> <|detail1->23,detail2->27,detail3->13|>|>, <| Cat2-> <|detail4->15,detail5->35|>|>, <| Cat3-> <|Detail6->56|>|>} 

Update is it possible to come up with something that works for N levels of nesting?

Eg 3 levels of nesting

list = {{"BigCat", "Category", "Detail", "Value"}, {"BigCat", "Cat1", "detail1", 23}, {"BigCat", "Cat1", "detail2", 27}, {"BigCat", "Cat1", "detail3", 13}, {"BigCat", "Cat2", "detail4", 15}, {"BigCat", "Cat2", "detail5", 35}, {"LittleCat", "Cat3", "Detail6", 56}} 
Gordon Coale
  • 2.4k
  • 17
  • 20