# Computed Column on Dataset

**URL:** <https://community.wolfram.com/t/computed-column-on-dataset/17166>\
**Category:** Questions\
**Tags:** data-science\
**Created:** [December 8, 2020, 7:07pm UTC](https://community.wolfram.com/t/computed-column-on-dataset/17166 "2020-12-08T19:07:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablogilz](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pablogilz](https://community.wolfram.com/u/pablogilz)\
**Post date:** [December 8, 2020, 7:07pm UTC](https://community.wolfram.com/t/computed-column-on-dataset/17166/1 "2020-12-08T19:07:23Z")

</div>

Can someone tell me how to write a Query that adds some columns, computed from several other columns in a Dataset?

---

<div class="post-metadata">

**Author:** ![murta](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/murta/32/7023_2.png) [@murta](https://community.wolfram.com/u/murta)\
**Post date:** [December 9, 2020, 1:32am UTC](https://community.wolfram.com/t/computed-column-on-dataset/17166/2 "2020-12-09T01:32:18Z")

</div>

A simple way:

```
data = Dataset[{<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}]
data[All, <|"f1" -> #a^2 + #b, "f2" -> N[Sin[#b + #a]]|> &]

```

If you want to append the collumn:

```
data[All, <|#, "f1" -> #a^2 + #b, "f2" -> N[Sin[#b + #a]]|> &]

```

---

<div class="post-metadata">

**Author:** ![pablogilz](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pablogilz](https://community.wolfram.com/u/pablogilz)\
**Post date:** [December 9, 2020, 5:19am UTC](https://community.wolfram.com/t/computed-column-on-dataset/17166/3 "2020-12-09T05:19:44Z")

</div>

ok, I was wrong, I was handling a List of associations like a Dataset, for a List you have to use a Query function, nor for a Dataset, as I said in my question

```
data1 = {<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>};

```

now appending two new computed columns to the List:

```
Query[All, {"a", "b"} /* <| "a" -> #a &, "b" -> #b &, 
    "f1" -> #a ^2 + #b &, "f2" -> N[Sin[#b + #a]] & |>]@data1

```

many thanks Rodrigo

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [December 9, 2020, 2:14pm UTC](https://community.wolfram.com/t/computed-column-on-dataset/17166/4 "2020-12-09T14:14:43Z")

</div>

Hi Pablo,

Appending columns (which @Rodrigo mentioned) can also be done with `Query`.

```
Query[All, {"a", "b"} /* (<|#, "f1" -> #a^2 + #b, "f2" -> N[Sin[#b + #a]]|> &)]@data1

(* <|"a" -> 1, "b" -> 2, "f1" -> 3, "f2" -> 0.14112|>, 
   <|"a" -> 3, "b" -> 4, "f1" -> 13, "f2" -> 0.656987|>} *)

```

---

<div class="post-metadata">

**Author:** ![pablogilz](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pablogilz](https://community.wolfram.com/u/pablogilz)\
**Post date:** [December 9, 2020, 3:28pm UTC](https://community.wolfram.com/t/computed-column-on-dataset/17166/5 "2020-12-09T15:28:58Z")

</div>

I’m confused, I get an error the first time I execute this, then if I execute In[3] again there’s no error

```
In[1]:= data = {<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}
In[2]:= ds = Dataset[data]
In[3]:= Query[All, {"a", "b"} /* <| #&, "f1" -> #a ^2 + #b &, "f2" -> N[Sin[#b + #a]] & |>]@ds

```

however this works fine

```
In[1]:= data = {<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}
In[2]:= ds = Dataset[data]
In[3]:= Query[All, {"a", "b"} /* <| #&, "f1" -> #a ^2 + #b &, "f2" -> N[Sin[#b + #a]] & |>]@data

```

this is what motivated my comment above

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [December 9, 2020, 6:18pm UTC](https://community.wolfram.com/t/computed-column-on-dataset/17166/6 "2020-12-09T18:18:01Z")

</div>

The pure function has to be defined correctly

```
data = {<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}
ds = Dataset[data];

```

`Query` on `Dataset`

```
Query[All, {"a", "b"} /* (<|#, "f1" -> #a^2 + #b, "f2" -> N[Sin[#b + #a]]|> &)]@ds

```

`Query` on `Association`

```
Query[All, {"a", "b"} /* (<|#, "f1" -> #a^2 + #b, "f2" -> N[Sin[#b + #a]]|> &)]@data

```

In both cases the `Query` expression is identical

```
qFun = Query[All, {"a", "b"} /* (<|#, "f1" -> #a^2 + #b, "f2" -> N[Sin[#b + #a]]|> &)]

qFun@ds
qFun@data

```

---

<div class="post-metadata">

**Author:** ![pablogilz](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pablogilz](https://community.wolfram.com/u/pablogilz)\
**Post date:** [December 9, 2020, 6:50pm UTC](https://community.wolfram.com/t/computed-column-on-dataset/17166/7 "2020-12-09T18:50:37Z")

</div>

Ok, it works fine, Thank you Rohit.
