Skip to main content
added 6 characters in body
Source Link
SquareOne
  • 7.7k
  • 1
  • 17
  • 36

If you need a method for Nany levels of nesting (as mentionned in your update), maybe try something like this:

groupbyNest[x_List] := GroupBy[x, First -> Rest, groupbyNest] /; Last@Dimensions@x > 1 groupbyNest[x_List] := First@Flatten@x /; Last@Dimensions@x == 1 

Then for example, given

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}} 

just

groupbyNest@Rest@list 

returns

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

If you need a method for N levels of nesting (as mentionned in your update), maybe try something like this:

groupbyNest[x_List] := GroupBy[x, First -> Rest, groupbyNest] /; Last@Dimensions@x > 1 groupbyNest[x_List] := First@Flatten@x /; Last@Dimensions@x == 1 

Then for example, given

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}} 

just

groupbyNest@Rest@list 

returns

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

If you need a method for any levels of nesting (as mentionned in your update), maybe try something like this:

groupbyNest[x_List] := GroupBy[x, First -> Rest, groupbyNest] /; Last@Dimensions@x > 1 groupbyNest[x_List] := First@Flatten@x /; Last@Dimensions@x == 1 

Then for example, given

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}} 

just

groupbyNest@Rest@list 

returns

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

Source Link
SquareOne
  • 7.7k
  • 1
  • 17
  • 36

If you need a method for N levels of nesting (as mentionned in your update), maybe try something like this:

groupbyNest[x_List] := GroupBy[x, First -> Rest, groupbyNest] /; Last@Dimensions@x > 1 groupbyNest[x_List] := First@Flatten@x /; Last@Dimensions@x == 1 

Then for example, given

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}} 

just

groupbyNest@Rest@list 

returns

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