2
$\begingroup$

I would like to sort my Dataset by its row index. Unlike an ordinary column, this index does not have a name.

myDataSet=Dataset[{Association["c1"->"b","c2"->1],Association["c1"->"a","c2"->2],Association["c1"->"a","c2"->3],Association["c1"->"c","c2"->5],Association["c1"->"c","c2"->7],Association["c1"->"b","c2"->4]}]

enter image description here

myDataSet[GroupBy["c1"],KeyDrop["c1"]]

enter image description here

The expected order of the Dataset is "a", "b", "c".

In the case of multiple row indices, it get's even more complicated.

myDataSet2=Dataset[{Association["c0"->"2","c1"->"b","c2"->1],Association["c0"->"2","c1"->"b","c2"->2],Association["c0"->"2","c1"->"a","c2"->3],Association["c0"->"2","c1"->"a","c2"->4],Association["c0"->"1","c1"->"a","c2"->1],Association["c0"->"1","c1"->"a","c2"->2],Association["c0"->"1","c1"->"b","c2"->3],Association["c0"->"1","c1"->"b","c2"->4]}]

enter image description here

myDataSet2[GroupBy["c0"],GroupBy["c1"],KeyDrop["c0"],KeyDrop["c1"]]

enter image description here

The expected order is "1", "2", and within each level "a", "b".

Update

Following the answer by kglr, the KeySortmethod does work for Datasets with a single level index, but does not work directly for multiple indices as it only operates on the first level.

myDataSet2[GroupBy["c0"],GroupBy["c1"],KeyDrop["c0"],KeyDrop["c1"]][KeySort]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ KeySort@myDataSet[GroupBy["c1"],KeyDrop["c1"]]? $\endgroup$ – kglr Sep 5 at 10:29
  • $\begingroup$ myDataSet[GroupBy["c1"],KeyDrop["c1"]][KeySort] Thanks $\endgroup$ – LBogaardt Sep 5 at 10:54
3
$\begingroup$
KeySort @ myDataSet[GroupBy["c1"],KeyDrop["c1"]]

enter image description here

Also

myDataSet[GroupBy["c1"]/*KeySort,KeyDrop["c1"]]

same picture

Update: Multi-level grouping:

myDataSetb = Dataset[{Association["c0" -> "1", "c1" -> "b", "c2" -> 1], 
   Association["c0" -> "1", "c1" -> "b", "c2" -> 2], 
   Association["c0" -> "1", "c1" -> "a", "c2" -> 3], 
   Association["c0" -> "1", "c1" -> "a", "c2" -> 4], 
   Association["c0" -> "0", "c1" -> "a", "c2" -> 1], 
   Association["c0" -> "0", "c1" -> "a", "c2" -> 2], 
   Association["c0" -> "0", "c1" -> "b", "c2" -> 3], 
   Association["c0" -> "0", "c1" -> "b", "c2" -> 4]}];

grouped = myDataSetb[GroupBy["c0"], GroupBy["c1"]]

grouped[Sort, ReverseSort]

enter image description here

$\endgroup$
  • $\begingroup$ +1 for /*KeySort. I've never seen /*before. The Help tells me it's RightComposition. That's quite elegant. $\endgroup$ – LBogaardt Sep 5 at 11:04
  • $\begingroup$ What about multi-level row indices? myDataSet=Dataset[{Association["c0"->"1","c1"->"b","c2"->1],Association["c0"->"1","c1"->"b","c2"->2],Association["c0"->"1","c1"->"a","c2"->3],Association["c0"->"1","c1"->"a","c2"->4],Association["c0"->"0","c1"->"a","c2"->1],Association["c0"->"0","c1"->"a","c2"->2],Association["c0"->"0","c1"->"b","c2"->3],Association["c0"->"0","c1"->"b","c2"->4]}] In that case, KeySort only sorts the first index. $\endgroup$ – LBogaardt Sep 5 at 11:21
  • $\begingroup$ @LBogaardt, you mean its grouped version myDataSet[GroupBy["c0"],GroupBy["c1"],KeyDrop["c1"]]? $\endgroup$ – kglr Sep 5 at 11:33
  • $\begingroup$ Yeah, sorry, I should have added this to my Question with more details. Indeed, say I have multiple levels, how can I sort on all levels? Preferably, even indicating Asc or Desc per index level. Similar to SQL or pandas etc. $\endgroup$ – LBogaardt Sep 5 at 11:45
  • $\begingroup$ I updated the original post. I'll keep your answer as 'accepted'. $\endgroup$ – LBogaardt Sep 5 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.