6

What I am trying to do is to add an id column at the left side of my data set.

This is the data set I am working with. I Have three columns and I want to add a new column on the left side with the column tag ID with the following values: Id={1,2,3,4,5,6}.

Dataset@
 {
  <|"Team name" -> "Chealse", "GF" -> 23, "GA" -> 12|>,
  <|"Team name" -> "Arsenal", "GF" -> 30, "GA" -> 13|>,
  <|"Team name" -> "Burnley", "GF" -> 33, "GA" -> 15|>,
  <|"Team name" -> "Everton", "GF" -> 53, "GA" -> 11|>,
  <|"Team name" -> "Watford", "GF" -> 45, "GA" -> 9|>,
  <|"Team name" -> "Brighton", "GF" -> 24, "GA" -> 7|>
  }
user1066
  • 17,923
  • 3
  • 31
  • 49
Las Des
  • 807
  • 4
  • 9

2 Answers2

7
Join[Dataset[Association /@ Thread["ID" -> Id]], ds, 2]

enter image description here

Also

MapIndexed[Prepend[#, "ID" -> #2[[1]]] &, ds]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • It worked fine, but I don´t understand the 2 on the Join function. – Las Des May 31 '20 at 22:33
  • 2
    @LasDes, the third argument of Join specifies that the elements at Level 2 of the objects in the first and second argument are to be joined. Try, for example, Join[{{a}, {b}, {c}}, {{1}, {2}, {3}}] and Join[{{a}, {b}, {c}}, {{1}, {2}, {3}}, 2] – kglr May 31 '20 at 22:37
  • I can see now why, thanks. – Las Des May 31 '20 at 22:42
2

Another way

ds[MapIndexed[<|"ID" -> First@#2, #|> &]]

enter image description here

Rohit Namjoshi
  • 10,212
  • 6
  • 16
  • 67