Message Boards Message Boards

GROUPS:

[✓] Add a column of a list of values to a dataset?

Posted 5 months ago
725 Views
|
3 Replies
|
3 Total Likes
|

How can I add a column into my data set. This is the data set:

dt = Dataset@
{
<|"col1" -> 2, "col2" -> 23, "col3" -> 12|>,
<|"col1" -> 3, "col2" -> 30, "col3" -> 13|>,
<|"col1" -> 5, "col2" -> 33, "col3" -> 15|>,
<|"col1" -> 34, "col2" -> 53, "col3" -> 11|>,
<|"col1" -> 43, "col2" -> 45, "col3" -> 9|>,
<|"col1" -> 11, "col2" -> 24, "col3" -> 7|>
}

I have been trying using dt[All, Prepend["col4" -> 1]] but this adds a column with the same values, And I am trying to add a list of values as a column like col4 = {1,34,65,76,8,66}. Any thoughts?

3 Replies
Append[dt[Transpose], "col4" -> {1, 34, 65, 76, 8, 66}][Transpose]
Posted 5 months ago

Another way, without using Transpose

dt[MapIndexed[<|"col4" -> Extract[col4, #2], #|> &]]

Love this one-liner code!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract