# Why this syntax for renaming columns in datasets?

Form the documentation of Dataset:

    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" -> {}|>}]


gives a Dataset with columns "a", "b" and "c". To rename a column in a Dataset one uses the syntax

 dataset[All,<|"New"->"old"|>]


where "old" is the old column name and "New" is the new one. Here (also from the documentation):

dataset[All, <|"A" -> "a", "B" -> "b", "C" -> "c"|>]


and one gets the new Dataset with columns "A", "B" and "C". I wonder why this is in this way and not:

 dataset[All,<|"old"->"New"|>]


Can one give me a hint?

• The "old" is implicitly #["old"]. With that in mind, #["old"]->"New" is not what we want; "New"->#["old"] is. – b3m2a1 Dec 25 '17 at 22:34

Just came across the same problem and find the following might be a sensible way:

dataset[All,KeyMap[Replace["old" -> "New"]]]


This approach essentially conducts the idea of OP and reserves the names of other columns.

BTW, I find the way suggested by the official documentation to rename columns (i.e., using Association as the operator) seems a bit tedious as unconnected columns are also involved to construct a new Association.

The easiest way to think about the short-hand query syntax is to turn to the documentation of Query, where we find that Query[All,f] will apply the function f to every row. So Query[All, <|"aaa" -> #"a"|> &] would be the natural way to write a query. The way you wrote it (i.e., Query[All, <|"aaa" -> "a"|> ]) is allowed as a more compact expression of the same query.

(Btw, this is not really "renaming", since it produces a new dataset.)

• But... I do not see how this answers the main question: why "New"->"old" instead of "old"->"New"? – Alexey Popkov Dec 25 '17 at 20:04
• @AlexeyPopkov Again, the reason is that you are effectively using a shorthand for a function definition, where this kind of function allows you to extract values from an association by using the keys. Again, this is really not renaming: it is effectively creating a new dataset by mapping this function across the rows of the old dataset. Try it that way to see: <|"aaa" -> #"a"|> & /@ dataset. You can use any function you want to, of course, but try to think of one that would use the notation you suggest... – Alan Dec 26 '17 at 14:53
• Thanks, now I got it! – Alexey Popkov Dec 26 '17 at 15:03