Message Boards Message Boards

0
|
4369 Views
|
3 Replies
|
3 Total Likes
View groups...
Share
Share this post:

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

Posted 4 years ago

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?

POSTED BY: J V. Alva
3 Replies
Posted 4 years ago

Another way, without using Transpose

dt[MapIndexed[<|"col4" -> Extract[col4, #2], #|> &]]
POSTED BY: Rohit Namjoshi

Love this one-liner code!

POSTED BY: Mads Bahrami
Append[dt[Transpose], "col4" -> {1, 34, 65, 76, 8, 66}][Transpose]
POSTED BY: Mads Bahrami
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