5
$\begingroup$

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:

enter image description here

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.

$\endgroup$

3 Answers 3

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

enter image description here

Also

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

same result

$\endgroup$
7
$\begingroup$

Like this:

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

?

$\endgroup$
3
$\begingroup$

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.

enter image description here

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):

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ you can use dsBracket[All,KeyValueMap[<|"RowKeys" -> #, #2|>&]] and dsNoBracket[KeyValueMap[<|"RowKeys" -> #, #2|>&]] for the two cases. $\endgroup$
    – kglr
    Commented Aug 6, 2019 at 21:46
  • 1
    $\begingroup$ 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? $\endgroup$
    – Kuba
    Commented Aug 7, 2019 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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