# Difference between df.column and df\[!, :column\]

**URL:** <https://discourse.julialang.org/t/difference-between-df-column-and-df-column/43530>\
**Category:** New to Julia\
**Tags:** question, dataframes\
**Created:** [July 23, 2020, 1:37am UTC](https://discourse.julialang.org/t/difference-between-df-column-and-df-column/43530 "2020-07-23T01:37:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauloat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauloat/32/16517_2.png) [@pauloat](https://discourse.julialang.org/u/pauloat)\
**Post date:** [July 23, 2020, 1:37am UTC](https://discourse.julialang.org/t/difference-between-df-column-and-df-column/43530/1 "2020-07-23T01:37:56Z")

</div>

Hi, im seeing this two ways to call a column and testing into code cant see any difference, is one better that the other?

thanks.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [July 23, 2020, 2:31am UTC](https://discourse.julialang.org/t/difference-between-df-column-and-df-column/43530/2 "2020-07-23T02:31:42Z")

</div>

Basically no difference, see [Indexing · DataFrames.jl](https://juliadata.github.io/DataFrames.jl/stable/lib/indexing/)

Both of them provide direct access to `column` (without copying)

---

<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:** [July 23, 2020, 6:40am UTC](https://discourse.julialang.org/t/difference-between-df-column-and-df-column/43530/3 "2020-07-23T06:40:39Z")

</div>

In addition to this, the 2nd syntax can be used for the creation of new columns and for accessing column names stored in a variable, whereas the 1st syntax can only be used to access an existing column which name is known in advance.

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 23, 2020, 7:11am UTC](https://discourse.julialang.org/t/difference-between-df-column-and-df-column/43530/4 "2020-07-23T07:11:42Z")

</div>

I think the first syntax may not work (as far as I know) if the column name has a space (or similar)

---

<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:** [July 23, 2020, 7:24am UTC](https://discourse.julialang.org/t/difference-between-df-column-and-df-column/43530/5 "2020-07-23T07:24:47Z")

</div>

It works if you quote the name:

```julia
df[!, "my col"] = [1,2]
df."my col"

```

---

<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:** [July 23, 2020, 7:41am UTC](https://discourse.julialang.org/t/difference-between-df-column-and-df-column/43530/6 "2020-07-23T07:41:09Z")

</div>

This isn’t quite right:

```julia
julia> using DataFrames

julia> df = DataFrame(a = rand(3))
3×1 DataFrame
│ Row │ a │
│ │ Float64 │
├─────┼──────────┤
│ 1 │ 0.689493 │
│ 2 │ 0.339562 │
│ 3 │ 0.707628 │

julia> df.b = rand(3); df
3×2 DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 0.689493 │ 0.25931 │
│ 2 │ 0.339562 │ 0.76706 │
│ 3 │ 0.707628 │ 0.288513 │

julia> df.c .= 1
ERROR: ArgumentError: column name :c not found in the data frame; existing most similar names are: :a and :b
Stacktrace:
 [1] lookupname at C:\Users\ngudat\.julia\packages\DataFrames\htZzm\src\other\index.jl:289 [inlined]
 [2] getindex at C:\Users\ngudat\.julia\packages\DataFrames\htZzm\src\other\index.jl:295 [inlined]
 [3] getindex(::DataFrame, ::typeof(!), ::Symbol) at C:\Users\ngudat\.julia\packages\DataFrames\htZzm\src\dataframe\dataframe.jl:435
 [4] getproperty(::DataFrame, ::Symbol) at C:\Users\ngudat\.julia\packages\DataFrames\htZzm\src\abstractdataframe\abstractdataframe.jl:346
 [5] top-level scope at REPL[13]:100:

```

As you see, `df.b = rand(3)` works despite `:b` not being present in the DataFrame. This only fails when doing dot-broadcasting assignment `.=`, because as per the link above:

> `df.col` works like `df[!, col]` […] in all cases except that `df.col .= v` and `sdf.col .= v` perform in-place broadcasting if `col` is present in `df` / `sdf` and is a valid identifier.

---

<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:** [July 23, 2020, 7:43am UTC](https://discourse.julialang.org/t/difference-between-df-column-and-df-column/43530/7 "2020-07-23T07:43:18Z")

</div>

See [https://github.com/JuliaLang/julia/issues/36741](https://github.com/JuliaLang/julia/issues/36741) for a discussion about allowing this.

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 23, 2020, 8:57am UTC](https://discourse.julialang.org/t/difference-between-df-column-and-df-column/43530/8 "2020-07-23T08:57:34Z")

</div>

Thanks. I did not know that. Very helpful. (much shorter syntax!)  
I actually was not aware that there is some implicit promotion from string to symbol here.

Interesting (I just read the other interesting thread about symbols here [When should a function accept a symbol as an argument?](https://discourse.julialang.org/t/when-should-a-function-accept-a-symbol-as-an-argument/43510))

---

<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:** [July 23, 2020, 10:29am UTC](https://discourse.julialang.org/t/difference-between-df-column-and-df-column/43530/9 "2020-07-23T10:29:34Z")

</div>

@nilshg @bkamins  
Thanks for the information!  
I was always wondering why the df.column syntax has often not worked for me when new columns should be created…

---

<div class="post-metadata">

**Author:** ![pauloat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauloat/32/16517_2.png) [@pauloat](https://discourse.julialang.org/u/pauloat)\
**Post date:** [July 23, 2020, 11:40pm UTC](https://discourse.julialang.org/t/difference-between-df-column-and-df-column/43530/10 "2020-07-23T23:40:06Z")

</div>

I was using Symbol(“my column”) to access the columns with spaces. didn’t knew you can also do that.
