Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name columns in dataframes using a string in Julia?

I am trying to find a way to name columns of a dataframe using strings coming from excel or scraping the web.

So how to transform "colname" to colname below?

df = DataFrame(colname = [1, 2])

I tried

df = DataFrame(symbol("colname") = [1, 2])

or

df = DataFrame([1, 2], [symbol("colname")])

and many other combinations, but no success. I see questions related to deleting columns based on string column names but no question/answer for naming columns in the first place.

like image 287
Ferenc Avatar asked Aug 30 '15 02:08

Ferenc


1 Answers

May be you can try something like this in two steps using the names! function.

using DataFrames
newname = ["colname1", "colname2"]
df = DataFrame(v1 = [1, 2], v2 = [3, 4])
names!(df.colindex, map(parse, newname))
df
# 2x2 DataFrames.DataFrame
# | Row | colname1 | colname2 |
# |-----|----------|----------|
# | 1   | 1        | 3        |
# | 2   | 2        | 4        |

Here are the version of Julia and DataFrames.jl I used

versioninfo()
# Julia Version 0.4.0-dev+6991
# Commit 811a977 (2015-08-26 04:02 UTC)
# Platform Info:
#   System: Linux (x86_64-unknown-linux-gnu)
#   CPU: Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz
#   WORD_SIZE: 64
#   BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
#   LAPACK: libopenblas
#   LIBM: libopenlibm
#   LLVM: libLLVM-svn


Pkg.installed("DataFrames")
# v"0.6.9"
like image 136
dickoa Avatar answered Oct 20 '22 00:10

dickoa