I want to combine two datasets that share row keys but have different column keys. Visually, this looks like a JoinAcross on the row keys to me, but the column of row keys doesn't appear to be named. Can I perform this combination as a JoinAcross, or does it require a different function?

Also, the row keys are sometimes cumbersome--is there a way to demote them to normal rows?

Example of desired dataset combination

I have two datasets:

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

I want the join these such that I get a third dataset:

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

I have tried JoinAcross[ dsA, dsB, Keys[dsA]], but this produces unclear errors.

Example of desired Key demotion

I have dsA again. I want to "demote" its row keys, such that I get:

{<|"RowKeys" -> "keyA", "key1" -> "a", "key2" -> "b"|>,
  <|"RowKeys" -> "keyB", "key1" -> "c", "key2" -> "d"|>} // Dataset

2 Answers 2


Join at level 3:

dsC = Join[dsA, dsB, 3]

enter image description here

For the second part of the question:

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

enter image description here

Apply[KeyValueMap[<|"RowKeys" -> #, #2|>&]] @ dsC

enter image description here

  • $\begingroup$ The Apply method works for a two row dataset, but appears to fail for datasets with more than two rows. For example, if I say dsC=Join[dsA,dsB,3] and then use the above Apply method, I get a Failure: KeyValueMap[Association[RowKeys->#1,#2]&] called with 2 arguments; 1 argument is expected. $\endgroup$
    – KBL
    Aug 3, 2019 at 1:51
  • $\begingroup$ @KBL, posted what i get when I use Apply[KeyValueMap[<|"RowKeys" -> #, #2|>&]] on dsC on v12 /Wolfram Cloud. $\endgroup$
    – kglr
    Aug 3, 2019 at 2:01
  • $\begingroup$ you are right, I typed the wrong thing. What I meant to type was that the Apply method doesn't work when there are more than 2 rows--if I have dsD=Join[dsA,dsB] (without the 3) and type Apply[KeyValueMap[<|"RowKeys" -> #, #2|>&]]@dsD, I get KeyValueMap[Association[RowKeys->#1,#2]&] called with 2 arguments; 1 argument is expected. $\endgroup$
    – KBL
    Aug 5, 2019 at 16:01
  • $\begingroup$ @KBL, looks like changing Apply to Map works for both cases. $\endgroup$
    – kglr
    Aug 6, 2019 at 16:14
  • 1
    $\begingroup$ Thanks very much for the answers here and on the other question. As I posted on the other page, I found a nasty dataset wrinkle that was causing my dataset to be especially uncooperative. With your help, though, I have the answer. $\endgroup$
    – KBL
    Aug 6, 2019 at 18:39
    Merge[{Normal@dsA, Normal@dsB}, Join @@ # &] // Dataset

Your Answer

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

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