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

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.

Does this do what you want:

df[:,Symbol("Second-column")]
1 Like

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

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  
1 Like
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.

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

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.

1 Like

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.

In DataFramesMeta you can use $ to work with column names generated by code (which includes simple string values). See the documentation here.

Example:

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

3 Likes

This tutorial should get you up to speed with this syntax, if you haven’t worked through it already.

1 Like