# Specify AsTable output column names

**URL:** https://discourse.julialang.org/t/specify-astable-output-column-names/72742
**Category:** Data
**Tags:** dataframes
**Created:** [December 8, 2021, 4:21am UTC](https://discourse.julialang.org/t/specify-astable-output-column-names/72742 "2021-12-08T04:21:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [December 8, 2021, 4:21am UTC](https://discourse.julialang.org/t/specify-astable-output-column-names/72742/1 "2021-12-08T04:21:21Z")

</div>

Can I specify the name of the output column?

```julia
using DataFrames, Chain
@chain DataFrame(a=[1,2,3,1,2,3], b=[:x, :y, :z, :x, :y, :x]) begin
groupby(:a)
combine(:b=>unique=>:b, :b => (b->mean.(map(b₀->b₀.==b, unique(b)))) => AsTable)
end
4×3 DataFrame
 Row │ a b x1      
     │ Int64 Symbol Float64 
─────┼────────────────────────
   1 │ 1 x 1.0
   2 │ 2 y 1.0
   3 │ 3 z 0.5
   4 │ 3 x 0.5

```

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [December 8, 2021, 7:11am UTC](https://discourse.julialang.org/t/specify-astable-output-column-names/72742/2 "2021-12-08T07:11:31Z")

</div>

Yes you just need to return a “table” with column names.

With a named tuple:

```julia
combine(:b=>unique=>:b, :b => (b->(; c=mean.(map(b₀->b₀.==b, unique(b))))) => AsTable)

```

With a Dict:

```julia
combine(:b=>unique=>:b, :b => (b->Dict(:c=>mean.(map(b₀->b₀.==b, unique(b))))) => AsTable)

```

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [December 8, 2021, 7:31am UTC](https://discourse.julialang.org/t/specify-astable-output-column-names/72742/3 "2021-12-08T07:31:53Z")

</div>

Additionally in this case just the following works:

```julia
julia> @chain DataFrame(a=[1,2,3,1,2,3], b=[:x, :y, :z, :x, :y, :x]) begin
       groupby(:a)
       combine(:b=>unique=>:b, :b => (b->mean.(map(b₀->b₀.==b, unique(b)))) => :output)
       end
4×3 DataFrame
 Row │ a b output
     │ Int64 Symbol Float64
─────┼────────────────────────
   1 │ 1 x 1.0
   2 │ 2 y 1.0
   3 │ 3 z 0.5
   4 │ 3 x 0.5

```

or - if indeed you produce a table and want to give new names for its columns use:

```julia
julia> @chain DataFrame(a=[1,2,3,1,2,3], b=[:x, :y, :z, :x, :y, :x]) begin
       groupby(:a)
       combine(:b=>unique=>:b, :b => (b->mean.(map(b₀->b₀.==b, unique(b)))) => [:output])
       end
4×3 DataFrame
 Row │ a b output
     │ Int64 Symbol Float64
─────┼────────────────────────
   1 │ 1 x 1.0
   2 │ 2 y 1.0
   3 │ 3 z 0.5
   4 │ 3 x 0.5

```

(the latter is more useful if you had more than one column - for a single vector the former is sufficient)

What @sijo proposed is a general solution if you want `AsTable` as a target and want to specify the column names within a transformation function.
