Skip to main content

Append a list as a column in a dataset


I have a Dataset and a List, called dataset and intervals, and I would like to append the list as a new column of the dataset. I have tried:


ds = Dataset[{
<|"char" -> 1, "freq" -> 0.1|>,

<|"char" -> 2, "freq" -> 0.2|>,
<|"char" -> 3, "freq" -> 0.3|>,
<|"char" -> 0, "freq" -> 0.4|>
}]

original_dataset


intervals = {{0, 0.4}, {0.4, 0.7}, {0.7, 0.9}, {0.9, 1.}}
ds[All, <|#, "intervals" -> intervals|> &]

result_dataset



but this adds the whole list to every row, whereas I would like each item of the list to be added to a different row.


Can this be achieved?



Answer



One way:


ds // Transpose // Append["intervals" -> intervals] // Transpose

Mathematica graphics


In Mathematica it is usually easier to operate on rows, which is what this solution demonstrates. I would have done the same if I was working with lists too. However, as other answers show there are dataset specific solutions that might work better.


Comments