# Creating new dataframe column!

**URL:** https://discourse.julialang.org/t/creating-new-dataframe-column/59711
**Category:** General Usage
**Tags:** question, dataframes
**Created:** [April 21, 2021, 4:35am UTC](https://discourse.julialang.org/t/creating-new-dataframe-column/59711 "2021-04-21T04:35:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)
#### Post date: [April 21, 2021, 4:35am UTC](https://discourse.julialang.org/t/creating-new-dataframe-column/59711/1 "2021-04-21T04:35:57Z")

</div>

I am trying to create a new column for a dataframe, such that it appends values only when the columns from the other dataframe have similar values.

For example:

```julia
using DataFrames, Statistics
a = ["1.0"; "2.0"; "2.0"; "4.0"; "5.0"]
b = collect(range(1, 5, length=5))
df = DataFrame(:a=> a, :b=>b)

x = combine(DataFrames.groupby(df, :a), :b=>mean)
df[!, :b_new] = [x[:, 2] for i in df[:, :a]]

println(df)

Output:->
5×3 DataFrame
 Row │ a b b_new                     
         │ String Float64 Array…               
─────────┼────────────────────────────────────────────────
       1 │ 1.0 1.0 [1.0, 2.5, 4.0, 5.0]
       2 │ 2.0 2.0 [1.0, 2.5, 4.0, 5.0]
       3 │ 2.0 3.0 [1.0, 2.5, 4.0, 5.0]
       4 │ 4.0 4.0 [1.0, 2.5, 4.0, 5.0]
       5 │ 5.0 5.0 [1.0, 2.5, 4.0, 5.0]

```

However i want to produce this dataframe:

```julia
Output:->
5×4 DataFrame
 Row │ a b b_new                     
         │ String Float64 Float64               
─────────┼────────────────────────────────────────────────
       1 │ 1.0 1.0 1.0
       2 │ 2.0 2.0 2.5
       3 │ 2.0 3.0 2.5
       4 │ 4.0 4.0 4.0
       5 │ 5.0 5.0 5.0

```

May I know how can i achieve the expected output?

Thanks!!

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [April 21, 2021, 5:04am UTC](https://discourse.julialang.org/t/creating-new-dataframe-column/59711/2 "2021-04-21T05:04:10Z")

</div>

Could you amend your MWE so that it works?

```julia
julia> x = combine(DataFrames.groupby(df, :a), :c=>mean)
ERROR: ArgumentError: column name :c not found in the data frame; existing most similar names are: :a and :b

```

---

<div class="post-metadata">

### Author: ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)
#### Post date: [April 21, 2021, 5:28am UTC](https://discourse.julialang.org/t/creating-new-dataframe-column/59711/3 "2021-04-21T05:28:53Z")

</div>

Apologies!!  
I have edited the question, now it is working. 🙂

Thanks for the response!

---

<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: [April 21, 2021, 6:13am UTC](https://discourse.julialang.org/t/creating-new-dataframe-column/59711/4 "2021-04-21T06:13:36Z")

</div>

You can use `transform`:

```julia
julia> transform(groupby(df, :a), :b=>mean)
5×3 DataFrame
 Row │ a b b_mean  
     │ String Float64 Float64 
─────┼──────────────────────────
   1 │ 1.0 1.0 1.0
   2 │ 2.0 2.0 2.5
   3 │ 2.0 3.0 2.5
   4 │ 4.0 4.0 4.0
   5 │ 5.0 5.0 5.0

```

See [Comparison with Python/R/Stata · DataFrames.jl](https://dataframes.juliadata.org/stable/man/comparisons/) for a list of common operations like this one.
