# Add columns to DataFrame

**URL:** https://discourse.julialang.org/t/add-columns-to-dataframe/49305
**Category:** Data
**Tags:** dataframes
**Created:** [October 30, 2020, 1:21pm UTC](https://discourse.julialang.org/t/add-columns-to-dataframe/49305 "2020-10-30T13:21:16Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smith/32/13567_2.png) [@Smith](https://discourse.julialang.org/u/Smith)
#### Post date: [October 30, 2020, 1:21pm UTC](https://discourse.julialang.org/t/add-columns-to-dataframe/49305/1 "2020-10-30T13:21:16Z")

</div>

I want to add columns to a dataframe iteratively in a loop and assign them a name. The code below seems to add one column to the dataframe containing value of p at the last iteration and name of the column appears as colname instead of Test10.

```julia
Using DataFrames
df = DataFrame()
for i = 1:10
     p = rand(3)
     colname = "Test$i"
     df[!,:colname] = p
end

```

How to fix this so that the dataframe contains values of p for all iterations (i.e. one column for each iteration) along with column names (e.g. Test1,Test2,…Test10)?

---

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [October 30, 2020, 1:42pm UTC](https://discourse.julialang.org/t/add-columns-to-dataframe/49305/2 "2020-10-30T13:42:42Z")

</div>

> [@Smith](#):
>
> `df[!,:colname] = p`

`df[!, colname] = p` should work (without the `:` before colname).

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [October 30, 2020, 1:52pm UTC](https://discourse.julialang.org/t/add-columns-to-dataframe/49305/3 "2020-10-30T13:52:44Z")

</div>

Are you coming from Stata, by chance?

`:colname` is not the same as the variable `colname`. `:colname` is an object called a `Symbol` , which is kind of like a String but not quite.

---

<div class="post-metadata">

### Author: ![Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smith/32/13567_2.png) [@Smith](https://discourse.julialang.org/u/Smith)
#### Post date: [October 30, 2020, 2:38pm UTC](https://discourse.julialang.org/t/add-columns-to-dataframe/49305/4 "2020-10-30T14:38:46Z")

</div>

Thanks!

---

<div class="post-metadata">

### Author: ![Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smith/32/13567_2.png) [@Smith](https://discourse.julialang.org/u/Smith)
#### Post date: [October 30, 2020, 2:39pm UTC](https://discourse.julialang.org/t/add-columns-to-dataframe/49305/5 "2020-10-30T14:39:16Z")

</div>

No, I have not used Stata. Thanks for the clarification

---

<div class="post-metadata">

### Author: ![rmsmsgood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmsmsgood/32/20544_2.png) [@rmsmsgood](https://discourse.julialang.org/u/rmsmsgood)
#### Post date: [September 19, 2022, 4:47am UTC](https://discourse.julialang.org/t/add-columns-to-dataframe/49305/6 "2022-09-19T04:47:41Z")

</div>

For a constant `c`, use `.=` instead of `=`.

```julia
df[!,:colname] .= c

```
