Let’s say I have a dataframe that only holds number or missings.
How can I convert it to a Array{T, 2} ? Without the names / header
Thank you
using DataFrames
df = DataFrame(a=[ 1, 2, 3, 4, missing], b= [1, 2, 3, 4, 5])
df[:,:]
Let’s say I have a dataframe that only holds number or missings.
How can I convert it to a Array{T, 2} ? Without the names / header
Thank you
using DataFrames
df = DataFrame(a=[ 1, 2, 3, 4, missing], b= [1, 2, 3, 4, 5])
df[:,:]
Try this one
df1=convert(Matrix,df)
Matrix(df)
or Matrix{T}(df)
if you want to specify T
that is different that is automatically inferred.
In the given example, Matrix{T}(df) throws an error if T is not = “Any” because one of the value is missing. Matrix(df) also works fine.
You have to choose an appropriate T
of course. Note for example that Matrix{Union{Real, Missing}}(df)
will work fine.
Great thank you!
Something I learned when trying the same thing
If df has various types Int
and Float64
for me, convert(Matrix, df)
will give an error, but Matrix(df)
works and sets all as Float64
.
convert(Matrix, df)
is not supported - indepentent of the fact what columns the df
stores. You need to user the constructor.