1
$\begingroup$
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 want to join column 'b' and 'c'. So that we have dataset of column 'a' and 'b'.Column b contianin the values of both 'b' and 'c'

$\endgroup$
1
  • 4
    $\begingroup$ Is this what you need <|"a" -> #a, "b" -> {#b, #c}|> & /@ dataset $\endgroup$
    – Kuba
    Commented Aug 26, 2016 at 10:55

2 Answers 2

2
$\begingroup$

Here is one way:

dataset[All, <| "a" -> "a", "b" -> (Prepend[#c, #b] &) |>]

dataset screenshot

$\endgroup$
0
1
$\begingroup$

I'm praying there is a better way to do this, but this is my take:

First, you take the first column and store it:

a1 = dataset[All, {"a"}] // Normal

then, you extract the remaining two columns of interest:

cols = dataset[All, {"b", "c"}];

Finally, you merge the rows of the two columns and then create a new dataset with the original column "a" and the newly calculated second column (new "b"):

f[assoc_] := <|"b" -> Flatten[{assoc["b"], assoc["c"]}]|>
a2 = cols[All, f[#] &] // Normal

dataset2 = Dataset@Apply[Append[#1, #2] &, Transpose[{a1, a2}], 1]
$\endgroup$
2
  • 1
    $\begingroup$ Nothing wrong with the way you are doing it, but since you prayed about it, you can modify Kuba's method to get the same result: <|"a" -> #a, "b" -> Flatten@{#b, #c}|> & /@ dataset seems the same as your dataset2 $\endgroup$
    – Jason B.
    Commented Aug 26, 2016 at 16:11
  • $\begingroup$ As a matter of fact, I did, and yes, you're right, it does work the same. Thanks! $\endgroup$
    – user42582
    Commented Aug 26, 2016 at 16:15

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.