# Selecting DataFrame columns with Symbol Syntax for columns with non-alphabetical characters

**URL:** <https://discourse.julialang.org/t/selecting-dataframe-columns-with-symbol-syntax-for-columns-with-non-alphabetical-characters/70466>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [October 27, 2021, 10:07am UTC](https://discourse.julialang.org/t/selecting-dataframe-columns-with-symbol-syntax-for-columns-with-non-alphabetical-characters/70466 "2021-10-27T10:07:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DorianT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doriant/32/24897_2.png) [@DorianT](https://discourse.julialang.org/u/DorianT)\
**Post date:** [October 27, 2021, 10:07am UTC](https://discourse.julialang.org/t/selecting-dataframe-columns-with-symbol-syntax-for-columns-with-non-alphabetical-characters/70466/1 "2021-10-27T10:07:51Z")

</div>

```julia
using DataFrames

df = DataFrame(Firstcolumn = 1:5, Secondcolumn = 1:5)

rename!(df, ["Firstcolumn","Second-column"])

# this works just fine

df[:,:Firstcolumn]

# this does not work

df[:,:Second-column]

# also does not work :(

df[:,:(Second-column)]

# works, but I need the Symbol syntax for more complex transformation calls

df[:,"Second-column"]

```

Is there a way to escape the “Second-column” column name? As the DataFrame has many variables I ideally want to avoid renaming the columns.

---

<div class="post-metadata">

**Author:** ![Rudi79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rudi79/32/3884_2.png) [@Rudi79](https://discourse.julialang.org/u/Rudi79)\
**Post date:** [October 27, 2021, 10:31am UTC](https://discourse.julialang.org/t/selecting-dataframe-columns-with-symbol-syntax-for-columns-with-non-alphabetical-characters/70466/2 "2021-10-27T10:31:06Z")

</div>

Does this do what you want:

```julia
df[:,Symbol("Second-column")]

```

---

<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:** [October 27, 2021, 10:34am UTC](https://discourse.julialang.org/t/selecting-dataframe-columns-with-symbol-syntax-for-columns-with-non-alphabetical-characters/70466/3 "2021-10-27T10:34:58Z")

</div>

Why does string indexing fail? This should work fine in `transform`?

```julia
julia> transform!(df, ["Firstcolumn", "Secondcolumn"] => ((f, s) -> f.^2 ./ .√(s)) => "Output")
5×3 DataFrame
 Row │ Firstcolumn Secondcolumn Output   
     │ Int64 Int64 Float64  
─────┼─────────────────────────────────────
   1 │ 1 1 1.0     
   2 │ 2 2 2.82843 
   3 │ 3 3 5.19615 
   4 │ 4 4 8.0     
   5 │ 5 5 11.1803  

```

---

<div class="post-metadata">

**Author:** ![DorianT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doriant/32/24897_2.png) [@DorianT](https://discourse.julialang.org/u/DorianT)\
**Post date:** [October 27, 2021, 11:54am UTC](https://discourse.julialang.org/t/selecting-dataframe-columns-with-symbol-syntax-for-columns-with-non-alphabetical-characters/70466/4 "2021-10-27T11:54:48Z")

</div>

```julia
using DataFramesMeta

@transform(df, :Firstcolumn = :Firstcolumn .+ 1)

@transform(df, "Second-column" = "Second-column" .+ 1)

```

Sorry, I was not precise enough here I believe. But I was under the impression, that DataFramesMeta does not play nicely with strings in this regard?

Using regular transform seems reasonable to me, I was just hoping for some kind of easy escape magic.

---

<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:** [October 27, 2021, 12:10pm UTC](https://discourse.julialang.org/t/selecting-dataframe-columns-with-symbol-syntax-for-columns-with-non-alphabetical-characters/70466/5 "2021-10-27T12:10:40Z")

</div>

Ah right, it’s a DataFramesMeta issue - you’d have to ask @pdeffebach then 🙂

My assumption would be that strings should work in DataFramesMeta given they do in regular DataFrames and I believe the design philosophy of DataFramesMeta is to be consistent with the workings of plain DataFrames.

---

<div class="post-metadata">

**Author:** ![DorianT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doriant/32/24897_2.png) [@DorianT](https://discourse.julialang.org/u/DorianT)\
**Post date:** [October 27, 2021, 12:24pm UTC](https://discourse.julialang.org/t/selecting-dataframe-columns-with-symbol-syntax-for-columns-with-non-alphabetical-characters/70466/6 "2021-10-27T12:24:20Z")

</div>

I did some testing and this seems to work in the same cases for which @nilshg suggested to use strings (i.e. a DataFrame transform call), but it also breaks DataFramesMeta.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [October 27, 2021, 12:54pm UTC](https://discourse.julialang.org/t/selecting-dataframe-columns-with-symbol-syntax-for-columns-with-non-alphabetical-characters/70466/7 "2021-10-27T12:54:07Z")

</div>

In DataFramesMeta you can use `$` to work with column names generated by code (which includes simple string values). See the documentation [here](https://juliadata.github.io/DataFramesMeta.jl/stable/#dollar).

Example:

```julia
using DataFramesMeta

df = DataFrame(:Firstcolumn => 1:5, Symbol("Second-column") => 1:5)

@transform(df, $"Second-column" = $"Second-column" .+ 1)

5×2 DataFrame
 Row │ Firstcolumn Second-column 
     │ Int64 Int64         
─────┼────────────────────────────
   1 │ 1 2
   2 │ 2 3
   3 │ 3 4
   4 │ 4 5
   5 │ 5 6

```

---

<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 27, 2021, 1:55pm UTC](https://discourse.julialang.org/t/selecting-dataframe-columns-with-symbol-syntax-for-columns-with-non-alphabetical-characters/70466/8 "2021-10-27T13:55:54Z")

</div>

[This tutorial](https://juliadata.github.io/DataFramesMeta.jl/stable/dplyr/) should get you up to speed with this syntax, if you haven’t worked through it already.
