2
$\begingroup$

Suppose I have a dataset like the following

enter image description here

with some rows containing nothing but dashes.

Question: Is there a way to split this dataset into multiple datasets (with the same headers) at the "dash" points? In this particular example, there would be 3 datasets (the first one containing 2 rows, the second one containing 3 rows, and the third one containing 1 row), each with the labels "Name", "Duration", and "Notes".

$\endgroup$

1 Answer 1

2
$\begingroup$

If we have

ds = Dataset[{
   <|"a" -> 1, "b" -> 2|>,
   <|"a" -> 1, "b" -> 2|>,
   <|"a" -> 1, "b" -> 2|>,
   <|"a" -> "-----", "b" -> "-----"|>,
   <|"a" -> 1, "b" -> 2|>,
   <|"a" -> 1, "b" -> 2|>,
   <|"a" -> "-----", "b" -> "-----"|>,
   <|"a" -> 1, "b" -> 2|>
   }]

enter image description here

(obviously replace with your own dataset)

We can do the following:

Dataset /@ 
 Select[SplitBy[Normal[ds], #a != "-----" &], 
  First[#]["a"] != "-----" &]

enter image description here

It's a bit rough, but easily extended to be a bit more graceful.

First, we change the dataset into a list of associations, using Normal[ds]. Then we split that list using SplitBy to get successive runs of rows in the dataset that don't have "a" -> "-----" (You may want to be a bit more specific with this predicate depending on your dataset). Then, we select only the sublists that aren't the "separator" sublists with Select[..., First[#]["a"] != "-----"&]. Then we simply turn the resulting lists back into Datasets.

$\endgroup$

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.