Skip to main content
emphasized the "all columns case"; added a second example for the selected columns case
Source Link
WReach
  • 69.3k
  • 4
  • 165
  • 272

HereIf the you want to total all columns instead of just those on a select list, then the suggestion given by @Kuba is one wayappropriate:

ds[All, <| #, "colnew" -> Total@# |> &]

dataset screenshot

If you wish to total only the listed columns, then:

ds[All, <| #, "colnew" -> Total[#[[list]]] |> &]

For example:

list = {"col1", "col2", "col3"};

ds = AssociationThread[list->#]& /@ RandomInteger[10, {4, 3}] // Dataset

list = {"col1", "col2", "col3"}

ds[All, <| #, "colnew" -> Total[#[[list]]] |> &]

dataset screenshotdataset screenshot

If the you want to total all columns instead of just those on a select listOr, then the suggestion given by @Kuba is appropriateasking for an actual subset of columns:

list2 = {"col1", "col2"};

ds[All, <| #, "colnew" -> Total@#Total[#[[list2]]] |> &]

dataset screenshotdataset screenshot

Here is one way:

ds[All, <| #, "colnew" -> Total[#[[list]]] |> &]

For example:

ds = AssociationThread[list->#]& /@ RandomInteger[10, {4, 3}] // Dataset

list = {"col1", "col2", "col3"}

ds[All, <| #, "colnew" -> Total[#[[list]]] |> &]

dataset screenshot

If the you want to total all columns instead of just those on a select list, then the suggestion given by @Kuba is appropriate:

ds[All, <| #, "colnew" -> Total@# |> &]

dataset screenshot

If the you want to total all columns instead of just those on a select list, then the suggestion given by @Kuba is appropriate:

ds[All, <| #, "colnew" -> Total@# |> &]

dataset screenshot

If you wish to total only the listed columns, then:

ds[All, <| #, "colnew" -> Total[#[[list]]] |> &]

For example:

list = {"col1", "col2", "col3"};

ds = AssociationThread[list->#]& /@ RandomInteger[10, {4, 3}] // Dataset

ds[All, <| #, "colnew" -> Total[#[[list]]] |> &]

dataset screenshot

Or, asking for an actual subset of columns:

list2 = {"col1", "col2"};

ds[All, <| #, "colnew" -> Total[#[[list2]]] |> &]

dataset screenshot

Source Link
WReach
  • 69.3k
  • 4
  • 165
  • 272

Here is one way:

ds[All, <| #, "colnew" -> Total[#[[list]]] |> &]

For example:

ds = AssociationThread[list->#]& /@ RandomInteger[10, {4, 3}] // Dataset

list = {"col1", "col2", "col3"}

ds[All, <| #, "colnew" -> Total[#[[list]]] |> &]

dataset screenshot

If the you want to total all columns instead of just those on a select list, then the suggestion given by @Kuba is appropriate:

ds[All, <| #, "colnew" -> Total@# |> &]

dataset screenshot