Group Abstract Group Abstract

Message Boards Message Boards

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

Add a column to some Dataset

Posted 6 years ago

Hello everybody!

It's likely a silly question, but I can't find the way to add a column to some dataset. Consider:

dataset = Dataset[{
   <|"a" -> 1, "b" -> 3, "c" -> {1}|>,
   <|"a" -> 2, "b" -> 8, "c" -> {2, 3}|>,
   <|"a" -> 3, "b" -> 0, "c" -> {3}|>,
   <|"a" -> 4, "b" -> 1, "c" -> {4, 5}|>,
   <|"a" -> 5, "b" -> 12, "c" -> {5, 6, 7}|>,
   <|"a" -> 6, "b" -> -3, "c" -> {}|>}]

and compute

Prov = dataset[All, {#a - #b &}]

How could I build another dataset with Prov as a supplementary column, named "Prov" ,for instance?

Thanks

POSTED BY: Claude Mante
3 Replies

Try:

dataset[All, <|#, "Prov" -> #a - #b|> &]

For working with Dataset I found this nice post inspiring.

POSTED BY: Henrik Schachner

Hi Claude,

See if that's what you want to do:

dataset2 = {<|"a" -> 1, "b" -> 3, "c" -> {1}|>, <|"a" -> 2, "b" -> 8, 
    "c" -> {2, 3}|>, <|"a" -> 3, "b" -> 0, "c" -> {3}|>, <|"a" -> 4, 
    "b" -> 1, "c" -> {4, 5}|>, <|"a" -> 5, "b" -> 12, 
    "c" -> {5, 6, 7}|>, <|"a" -> 6, "b" -> -3, "c" -> {}|>};
Prov2 = Table[
   dataset2[[x, 1]] - dataset2[[x, 2]], {x, 1, Length@dataset2}];

Dataset@Join[{Join[Keys@dataset2[[1]], {"Prov"}]}, 
  Table[Join[(Keys@dataset2[[1]] /. dataset2)[[y]], {Prov2[[y]]}], {y,
     1, Length@Prov2}]]

im1

Maybe there is another way to do it, but you can do it this way...

POSTED BY: Claudio Chaib
Attachments:
POSTED BY: Claude Mante
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard