# Changing Row Keys into Normal Rows

I have a dataset with column and row keys:

dsA={<|"keyA" -> <|"key1" -> "a", "key2" -> "b"|>,
"keyB" -> <|"key1" -> "c",
"key2" -> "d"|>|>, <|"keyC" -> <|"key1" -> "a", "key2" -> "b"|>,
"keyB" -> <|"key1" -> "d", "key2" -> "f"|>|>}//Dataset


Because the row keys get in the way of a number of operations (such as joining with other datasets that might have a different number of rows), I want to change the dataset to the following form:

I asked this as an addendum to a different question, but so far no luck. I can create datasets of the values and the keys:

dsValues = dsA[Values]
dsKeys = dsA[Keys]


But I'm not sure how to put the dsKeys column in front of the dsValues table.

Map[KeyValueMap[<|"RowKeys" -> #, #2|>&]] @ dsA


Also

dsA[All, KeyValueMap[<|"RowKeys" -> #, #2|>&]]


same result

Like this:

dsA[Flatten, KeyValueMap[Prepend[#2, "keys" -> #] &]]


?

Thanks to @kglr and @Kuba for their correct answers--I post this longer response to highlight an issue with brackets in datasets that I wasn't aware of. Apparently, you can make datasets in a couple of different ways--with a leading { bracket or without. Let's say we have two datasets (slightly modified versions of the one from my original question):

dsBracket = {<|
"keyA" -> <|"key1" -> "a", "key2" -> "b"|>,
"keyB" -> <|"key1" -> "c", "key2" -> "d"|>,
"keyC" -> <|"key1" -> "a", "key2" -> "b"|>,
"keyD" -> <|"key1" -> "d", "key2" -> "f"|>|>} // Dataset


and

dsNoBracket = <|
"keyA" -> <|"key1" -> "a", "key2" -> "b"|>,
"keyB" -> <|"key1" -> "c", "key2" -> "d"|>,
"keyC" -> <|"key1" -> "a", "key2" -> "b"|>,
"keyD" -> <|"key1" -> "d", "key2" -> "f"|>|> // Dataset


Note that the only difference is the {} on dsBracket.

I didn't realize that my dataset was lacking that bracket because I imported data and manipulated it all within the dataset environment--I'd prefer to ignore the mess! The datasets SemanticImport with the {; the dataset first loses the curly brace when I SelectFirst the result of a GroupBy.

@Kuba's method works for dsBracket, but produces a failure for the dsNoBracket. With @kglr's answer, I realized something weird was happening. Here's what happens with and without the bracket (and my workaround for the issue):

• you can use dsBracket[All,KeyValueMap[<|"RowKeys" -> #, #2|>&]] and dsNoBracket[KeyValueMap[<|"RowKeys" -> #, #2|>&]] for the two cases. – kglr Aug 6 '19 at 21:46
• Well, you get what you asked for :) If the dataset structure is different then you need to adapt the solution. If you take a look at my and @kglr 's results with the original dsA you can apply Normal to see that our results differ. I added Flatten on purpose to get rid of nested lists. At the end the question is, what is your input and desired output exactly? – Kuba Aug 7 '19 at 7:52