# Create new column with first row of each id

**URL:** <https://discourse.julialang.org/t/create-new-column-with-first-row-of-each-id/76083>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [February 9, 2022, 2:10pm UTC](https://discourse.julialang.org/t/create-new-column-with-first-row-of-each-id/76083 "2022-02-09T14:10:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 9, 2022, 2:10pm UTC](https://discourse.julialang.org/t/create-new-column-with-first-row-of-each-id/76083/1 "2022-02-09T14:10:44Z")

</div>

Hi ,  
i want to create a new column with the first row of each id . this is the data i have

```julia
df = DataFrame()
df.id = repeat(1:3,3)
df.x1 = 1:9
df.x2 = 10:18
sort!(df,:id)

```

i want column x3 as first row of each id in column x1

```julia
9×4 DataFrame
 Row │ id x1 x2 x3    
     │ Int64 Int64 Int64 Int64 
─────┼────────────────────────────
   1 │ 1 1 10 1
   2 │ 1 4 13 1
   3 │ 1 7 16 1
   4 │ 2 2 11 2
   5 │ 2 5 14 2
   6 │ 2 8 17 2
   7 │ 3 3 12 3
   8 │ 3 6 15 3
   9 │ 3 9 18 3

```

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:** [February 9, 2022, 2:28pm UTC](https://discourse.julialang.org/t/create-new-column-with-first-row-of-each-id/76083/2 "2022-02-09T14:28:05Z")

</div>

```julia
julia> transform!(groupby(df, :id), :x1 => first)
9×4 DataFrame
 Row │ id x1 x2 x1_first 
     │ Int64 Int64 Int64 Int64    
─────┼───────────────────────────────
   1 │ 1 1 10 1
   2 │ 1 4 13 1
   3 │ 1 7 16 1
   4 │ 2 2 11 2
   5 │ 2 5 14 2
   6 │ 2 8 17 2
   7 │ 3 3 12 3
   8 │ 3 6 15 3
   9 │ 3 9 18 3

```
