Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia DataFrame column names

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?

like image 575
jjjjjj Avatar asked Oct 20 '25 09:10

jjjjjj


1 Answers

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)...)...)
like image 139
Dan Getz Avatar answered Oct 22 '25 04:10

Dan Getz