# Df\["col"\] syntax

**URL:** <https://discourse.julialang.org/t/df-col-syntax/67254>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [August 29, 2021, 1:04am UTC](https://discourse.julialang.org/t/df-col-syntax/67254 "2021-08-29T01:04:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![xinchin](https://avatars.discourse-cdn.com/v4/letter/x/54ee81/32.png) [@xinchin](https://discourse.julialang.org/u/xinchin)\
**Post date:** [August 29, 2021, 1:04am UTC](https://discourse.julialang.org/t/df-col-syntax/67254/1 "2021-08-29T01:04:15Z")

</div>

is it possible to have df[“col”] syntax in DataFrames.jl? currently it throws error

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 29, 2021, 1:06am UTC](https://discourse.julialang.org/t/df-col-syntax/67254/2 "2021-08-29T01:06:25Z")

</div>

`df[:, "col"]`

---

<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:** [August 29, 2021, 1:13am UTC](https://discourse.julialang.org/t/df-col-syntax/67254/3 "2021-08-29T01:13:34Z")

</div>

Also `df.col`, but you can’t use a variable for that, i.e. `x = "mycolumn"; df.'x'`.

I recommend working through the DataFrames documentation for this. See a tutorial [here](https://dataframes.juliadata.org/stable/man/basics/).

---

<div class="post-metadata">

**Author:** ![xinchin](https://avatars.discourse-cdn.com/v4/letter/x/54ee81/32.png) [@xinchin](https://discourse.julialang.org/u/xinchin)\
**Post date:** [August 29, 2021, 1:48am UTC](https://discourse.julialang.org/t/df-col-syntax/67254/4 "2021-08-29T01:48:27Z")

</div>

i have code which df[“col”] syntax is used, and for me it is more understandable. just wanted to know if this is possible, maybe not!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 29, 2021, 1:49am UTC](https://discourse.julialang.org/t/df-col-syntax/67254/5 "2021-08-29T01:49:45Z")

</div>

`df[mask1, mask2]` is general, the `:` in my example means take all the rows

---

<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:** [August 29, 2021, 1:51am UTC](https://discourse.julialang.org/t/df-col-syntax/67254/6 "2021-08-29T01:51:17Z")

</div>

No. This was allowed in earlier versions of DataFrames, but people were confused because lots of users saw DataFrames as a collection of rows, in which case `df[1]` should return the first _row_, not first _column_.

As a result, DataFrames.jl enforces an indexing syntax which makes the dimensions clear.

---

<div class="post-metadata">

**Author:** ![xinchin](https://avatars.discourse-cdn.com/v4/letter/x/54ee81/32.png) [@xinchin](https://discourse.julialang.org/u/xinchin)\
**Post date:** [August 29, 2021, 1:55am UTC](https://discourse.julialang.org/t/df-col-syntax/67254/7 "2021-08-29T01:55:37Z")

</div>

many things to read, i should do that 🤔

---

<div class="post-metadata">

**Author:** ![DataFrames](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DataFrames](https://discourse.julialang.org/u/DataFrames)\
**Post date:** [August 29, 2021, 2:18am UTC](https://discourse.julialang.org/t/df-col-syntax/67254/8 "2021-08-29T02:18:29Z")

</div>

```julia
 Base.getindex(df::DataFrame, col::AbstractString) = df[!, col]

```

---

<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:** [August 29, 2021, 8:46am UTC](https://discourse.julialang.org/t/df-col-syntax/67254/9 "2021-08-29T08:46:34Z")

</div>

or more generally:

```julia
Base.getindex(df::AbstractDataFrame, col::DataFrames.ColumnIndex) = df[!, col]

```

and then the same for `setindex!`, broadcasting, and broadcasting assignment.

However, note, as @pdeffebach commented that it is not supported on purpose. In Julia Base you have the following:

```julia
julia> x = [1 2; 3 4]
2×2 Matrix{Int64}:
 1 2
 3 4

julia> x[3]
2

```

DataFrames.jl is designed to be:

- consistent with Julia Base (so that once you learn Julia Base you mostly know how to use DataFrames.jl)
- throw an error when DataFrames.jl decides that what Julia Base does would confuse users - and the case you are asking about is exactly one of these scenarios as I assume when writing:

```julia
julia> df = DataFrame(x, :auto)
2×2 DataFrame
 Row │ x1 x2
     │ Int64 Int64
─────┼──────────────
   1 │ 1 2
   2 │ 3 4

julia> df[3]
ERROR: ArgumentError: syntax df[column] is not supported use df[!, column] instead

```

you prefer to get an error rather than getting `2` (as this is what you would get for a matrix), as getting `2` would probably be super confusing.
