I want to name the column names of blank DataFrame.
I am having trouble (re)naming multiple columns of a DataFrame in v0.6.
I've tried generating names:
df = DataFrame()
nms = [":x$i" for i in 1:2]
df[nms[1] = rand(10)]
df[nms[2] = rand(10)]
but the symbol requirement for names is not working with the $
macro. Does anyone have a fix?
df = DataFrame()
nms = [Symbol("x$i") for i in 1:2]
df[nms[1]] = rand(10)
df[nms[2]] = rand(10)
works and is most similar to code in question. The key is to index DataFrames with Julia Symbols and not Strings.
A more compact way to make df
is using a DataFrame constructor which takes the columns as parameters. There is one, but the parameters are named parameters, so you need to splat (i.e. ...
) a Dict to insert the parameters. The Dict itself needs to be generated programmatically. To maintain a specific order of the columns in the Dict, you need an OrderedDict. The outcome is:
using DataFrames, DataStructures
df = DataFrame(;OrderedDict((Symbol("x$i")=>rand(10) for i=1:3)...)...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With