19
$\begingroup$

Using the example in the documentation, how would I make a new dataset with the key "b" changed to key "h".

dataset = Dataset[{
   <|"a" -> 1, "b" -> "x", "c" -> {1}|>,
   <|"a" -> 2, "b" -> "y", "c" -> {2, 3}|>,
   <|"a" -> 3, "b" -> "z", "c" -> {3}|>,
   <|"a" -> 4, "b" -> "x", "c" -> {4, 5}|>,
   <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>,
   <|"a" -> 6, "b" -> "z", "c" -> {}|>}]

I tried:

dataset /. "b"-> "h"

and also

Normal[dataset] /. "b"-> "h"

Which don't work. This comes up when I get sums using GroupBy. I'm using code from Szabolcs which results in my getting the sums, but they have the same name as the original key. I still don't really understand the code I'm using so I don't know how to handle it there, if possible. Eventually I have to use a JoinAcross to merge these totals with the original detail, and I need separate key names.

Szabolcs code is:

sales[
GroupBy[#, KeyTake[{"Country", "Region", "BU", "Year"}] -> KeyTake["Sales"], Total] &
][Normal
][All, Apply[Join]]

Source of Szabolcs code

$\endgroup$

3 Answers 3

22
$\begingroup$

We can explicitly construct a new association with key names of our choosing:

dataset[All, <| "a" -> "a", "h" -> "b", "c" -> "c" |>]

dataset screenshot

Alternatively, a function could be applied to the keys:

dataset[All, KeyMap[# /. "b" -> "h" &, #] &]

dataset screenshot

Note that a bug in the V10.0.0 type system prevents us from using the operator form KeyMap[# /. "b" -> "h"&]. (2020 Update: in more recent versions we can also write KeyMap[Replace["b" -> "h"]]).

Or, we could explicitly add the key "h" and drop the key "b", although this will re-order the keys in the resultant association:

dataset[All, <| #, "h" -> #b |> & /* KeyDrop["b"]]

dataset screenshot

Or, we could split each association into its keys and values, operate upon the keys, and then thread the results back together into an assocation:

dataset[All, AssociationThread[(Keys@# /. "b" -> "h") -> Values@#] &]

dataset screenshot

$\endgroup$
5
  • 1
    $\begingroup$ I gave up on KeyMap because dataset[All,KeyMap[#/."b"->"h"&]] wouldn't work. Is this expected? $\endgroup$
    – C. E.
    Aug 28, 2014 at 17:40
  • 1
    $\begingroup$ @Pickett I believe it should work, and I reported a bug to WRI. Consider that Dataset[<| a -> 1 |>][KeyMap[f]] works, but Dataset[<| "a" -> 1 |>][KeyMap[f]] doesn't. Looks like a bug to me. $\endgroup$
    – WReach
    Aug 28, 2014 at 17:47
  • $\begingroup$ OK, thanks. You have my +1 of course. $\endgroup$
    – C. E.
    Aug 28, 2014 at 17:51
  • $\begingroup$ KeyMap[Replace["b" -> "h"]] is a little more concise. $\endgroup$ Oct 6, 2020 at 13:30
  • $\begingroup$ @SjoerdSmit Thanks, I have added a 2020 update. I am a little daunted at the thought of going back and reviewing all the older Dataset posts on the site for obsolete constructions. The ones dating back to 10.x are particularly suspect since back then the type system had to be hacked more often than not. $\endgroup$
    – WReach
    Oct 6, 2020 at 18:19
4
$\begingroup$
Dataset[Association /@ (Normal@Normal@dataset /. "b" -> "h")]

enter image description here

$\endgroup$
1
  • 4
    $\begingroup$ +1 This could also be expressed as dataset[All, Normal /* (# /. "b" -> "h" &) /* Association]. $\endgroup$
    – WReach
    Aug 28, 2014 at 14:58
-2
$\begingroup$
    dataset = {<|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>, <|"a" -> 6, 
   "b" -> 5, "c" -> 9, "d" -> 2|>};
    changeKeys[assoc_, suffix_] := KeyMap[# <> suffix &, assoc]
    changeKeys[#, "1"] & /@ dataset

It turns out the in the help file under datasets there is an answer but it currently doesn't work. There must be a bug. The code above works. If some of your datasets are one row (i.e. an association) be sure to put curly brackets around it.

$\endgroup$
1
  • 1
    $\begingroup$ The question was how to change specific key name, not add a suffix. Also, the dataset is not a Dataset[...]. $\endgroup$
    – Kuba
    Oct 6, 2020 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.