# Adding a new row to a DataFrame

**URL:** <https://discourse.julialang.org/t/adding-a-new-row-to-a-dataframe/1331>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [January 6, 2017, 5:17pm UTC](https://discourse.julialang.org/t/adding-a-new-row-to-a-dataframe/1331 "2017-01-06T17:17:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![richthanki](https://avatars.discourse-cdn.com/v4/letter/r/51bf81/32.png) [@richthanki](https://discourse.julialang.org/u/richthanki)\
**Post date:** [January 6, 2017, 5:17pm UTC](https://discourse.julialang.org/t/adding-a-new-row-to-a-dataframe/1331/1 "2017-01-06T17:17:43Z")

</div>

It’s quite surprising that the DataFrames package documentation doesn’t provide a canonical way of adding a new record to a df. On the web I see approaches using `push!``append!``vcat!` and `@data` macros. Could someone help me out?

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [January 7, 2017, 12:06pm UTC](https://discourse.julialang.org/t/adding-a-new-row-to-a-dataframe/1331/2 "2017-01-07T12:06:45Z")

</div>

Try this : create an array of the same length of your dataframe columns and then use  
` df[:colname]=array`

```julia
 julia> df
5x4 DataFrames.DataFrame
| Row | a | b | c | d |
|-----|---|----|----|----|
| 1 | 1 | 7 | 10 | 20 |
| 2 | 2 | 8 | 11 | 21 |
| 3 | 3 | 9 | 12 | 22 |
| 4 | 4 | 10 | 13 | 23 |
| 5 | 5 | 11 | 14 | 24 |

julia> df[:M1]=m1
5-element Array{Float16,1}:
 4.0
 5.0
 6.0
 7.0
 8.0

julia> df
5x5 DataFrames.DataFrame
| Row | a | b | c | d | M1 |
|-----|---|----|----|----|-----|
| 1 | 1 | 7 | 10 | 20 | 4.0 |
| 2 | 2 | 8 | 11 | 21 | 5.0 |
| 3 | 3 | 9 | 12 | 22 | 6.0 |
| 4 | 4 | 10 | 13 | 23 | 7.0 |
| 5 | 5 | 11 | 14 | 24 | 8.0 |

```

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [January 7, 2017, 12:15pm UTC](https://discourse.julialang.org/t/adding-a-new-row-to-a-dataframe/1331/3 "2017-01-07T12:15:08Z")

</div>

To add a row, you can merge 2 dataframes :

```julia
 julia> df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
4×2 DataFrames.DataFrame
│ Row │ A │ B │
├─────┼───┼─────┤
│ 1 │ 1 │ "M" │
│ 2 │ 2 │ "F" │
│ 3 │ 3 │ "F" │
│ 4 │ 4 │ "M" │

julia> df2 = DataFrame(A = 6, B = ["F"])
1×2 DataFrames.DataFrame
│ Row │ A │ B │
├─────┼───┼─────┤
│ 1 │ 6 │ "F" │

julia> append!(df,df2)
5×2 DataFrames.DataFrame
│ Row │ A │ B │
├─────┼───┼─────┤
│ 1 │ 1 │ "M" │
│ 2 │ 2 │ "F" │
│ 3 │ 3 │ "F" │
│ 4 │ 4 │ "M" │
│ 5 │ 6 │ "F" │

```

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [February 20, 2017, 2:37pm UTC](https://discourse.julialang.org/t/adding-a-new-row-to-a-dataframe/1331/4 "2017-02-20T14:37:53Z")

</div>

Another solution using push!() :

```julia
 julia> df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
4×2 DataFrames.DataFrame
│ Row │ A │ B │
├─────┼───┼─────┤
│ 1 │ 1 │ "M" │
│ 2 │ 2 │ "F" │
│ 3 │ 3 │ "F" │
│ 4 │ 4 │ "M" │

julia> push!(df,[6 "F"])

julia> df
5×2 DataFrames.DataFrame
│ Row │ A │ B │
├─────┼───┼─────┤
│ 1 │ 1 │ "M" │
│ 2 │ 2 │ "F" │
│ 3 │ 3 │ "F" │
│ 4 │ 4 │ "M" │
│ 5 │ 6 │ "F" │

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [May 30, 2021, 12:17am UTC](https://discourse.julialang.org/t/adding-a-new-row-to-a-dataframe/1331/5 "2021-05-30T00:17:05Z")

</div>

> [@Fred](#):
>
> `append!(df,df2)`

What if I want to add certain rows, and in all the other rows, I only want to add 0?

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [May 30, 2021, 12:46am UTC](https://discourse.julialang.org/t/adding-a-new-row-to-a-dataframe/1331/6 "2021-05-30T00:46:47Z")

</div>

maybe this works for you:

```julia
df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
defaultRow = (A=0,B="0")
push!(df, (defaultRow...,A=1))
push!(df, (defaultRow...,B="hello"))

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [May 31, 2021, 8:24pm UTC](https://discourse.julialang.org/t/adding-a-new-row-to-a-dataframe/1331/8 "2021-05-31T20:24:31Z")

</div>

I got that to work, this too.

df = DataFrame(A = 1:4, B = [“M”, “F”, “F”, “M”])  
defaultRow = (A=0,B=“0”)  
Macro d(a,b  
ex=quote  
push!(df, (defaultRow…,$a=$b))  
end  
end

@d(A,0)  
@d(B,“Hello”)

---

<div class="post-metadata">

**Author:** ![rapus95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapus95/32/3773_2.png) [@rapus95](https://discourse.julialang.org/u/rapus95)\
**Post date:** [June 1, 2021, 10:16am UTC](https://discourse.julialang.org/t/adding-a-new-row-to-a-dataframe/1331/9 "2021-06-01T10:16:40Z")

</div>

sidenote: use a code block for code ^^ there’s a clickable button for it next to the text field. Alternatively use triple backticks as begin and end markers. Backtick: ` (not to be confused with forward tick ´ or apostroph ')

````julia
```
code
```

````

---

<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:** [November 15, 2021, 8:34am UTC](https://discourse.julialang.org/t/adding-a-new-row-to-a-dataframe/1331/10 "2021-11-15T08:34:02Z")

</div>

> [@Fred](#):
>
> `df[:M1]=m1`

This synrtax is currently not supported. Use `df.M1` instead.
