DataFrame column names with symbols

I’m using DataFramesMeta and would like to calculate a new column called “p*”.

I’ve tried:
@transform(df, p* = … )
@transform(df, “p*” = … )

No luck.

I can make do with:
Disc[!,“p*”] .= …
But, not ideal.

Thanks!

I don’t think this has anything to do with Symbols, you’re just not using the transform syntax correctly, it should be source => transformation => destination:

julia> df = DataFrame(a = 1:3)
3×1 DataFrame
 Row │ a     
     │ Int64 
─────┼───────
   1 │     1
   2 │     2
   3 │     3

julia> transform!(df, :a => cumsum => "p*")
3×2 DataFrame
 Row │ a      p*    
     │ Int64  Int64 
─────┼──────────────
   1 │     1      1
   2 │     2      3
   3 │     3      6
2 Likes

@Ismam is using DataFramesMeta.jl

2 Likes

I think DataFramesMeta.jl doesn’t support weird characters in the name I think.

But with DFMacros.jl you can do

@transform(df, Symbol("p*" = … )

You can write ur code in ``` for better formatting

@transform(df, p* = … )
@transform(df, “p*” = … )
2 Likes

Thanks everyone. Lots of useful suggestions here!

DataFramesMeta absolutely supports creating column names with weird symbols. See the documentation Working with column names programmatically with cols .

You do

@transform(df, cols("p*") = ...)
3 Likes

Just curious,

how do you paste tables as text so nicely like that?

My tables look like this in Jupyter

5×2 DataFrame
Row │ x y
│ Int64 Int64?
─────────────────────
1 │ 1 missing
2 │ 2 4
3 │ 3 missing
4 │ 4 2
5 │ 5 1

image

I copy them from the REPL, which has plain text output. Jupyter is HTML output so some formatting info gets lost when you copy the text displayed in your browser.

2 Likes