First impression of DataFrames.jl

Hi,

Trying to step into Julia these days, so far so good. Really excited about the multiple dispatching and type hint. Got a few questions regarding DataFrames.

Currently I am using the prostate cancer dataset from here. It is a 97x11 dataset. In pandas, you can call the corr method on the dataframe itself to generate a correlation matrix of all columns. Is there a similar method that I can call to get that on the Julia side?

Thank you!

Hi! Thank you for using DataFrames.jl. What type and shape of output you would expect? If you want a Matrix then just do:

using Statistics
cor(Matrix(your_data_frame))

but things with calculating correlations are quite involved so depending on the details you want a specific answer might be different (in particular - do you have missing values and how do you want to handle them?).

EDIT: what type of correlation do you want to calculate (I assumed Pearson correlation coefficient)?

In DataConvenience.jl there is dfcor

dfcor(df) should work.

Hi! Thank you for the reply. I am looking for something like this:

And you are right, for now I am thinking of pearson correlation.

You can get it like this:

julia> using DataFrames, NamedArrays, Statistics

julia> df = DataFrame(x1=rand(10), x2=rand(10), x3=rand(10), x4=rand(10))
10Γ—4 DataFrame
β”‚ Row β”‚ x1       β”‚ x2       β”‚ x3        β”‚ x4         β”‚
β”‚     β”‚ Float64  β”‚ Float64  β”‚ Float64   β”‚ Float64    β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1   β”‚ 0.158358 β”‚ 0.942125 β”‚ 0.713538  β”‚ 0.630956   β”‚
β”‚ 2   β”‚ 0.374393 β”‚ 0.813797 β”‚ 0.504182  β”‚ 0.947029   β”‚
β”‚ 3   β”‚ 0.520227 β”‚ 0.542249 β”‚ 0.833646  β”‚ 0.609631   β”‚
β”‚ 4   β”‚ 0.928563 β”‚ 0.402397 β”‚ 0.444998  β”‚ 0.232351   β”‚
β”‚ 5   β”‚ 0.230898 β”‚ 0.824582 β”‚ 0.199968  β”‚ 0.00203982 β”‚
β”‚ 6   β”‚ 0.197203 β”‚ 0.84624  β”‚ 0.408122  β”‚ 0.636816   β”‚
β”‚ 7   β”‚ 0.168241 β”‚ 0.281407 β”‚ 0.665497  β”‚ 0.949534   β”‚
β”‚ 8   β”‚ 0.494666 β”‚ 0.39342  β”‚ 0.236596  β”‚ 0.522137   β”‚
β”‚ 9   β”‚ 0.431282 β”‚ 0.425107 β”‚ 0.0946223 β”‚ 0.584611   β”‚
β”‚ 10  β”‚ 0.639034 β”‚ 0.256714 β”‚ 0.4461    β”‚ 0.298986   β”‚

julia> struct NoPrint end; Base.show(::IO, ::NoPrint) = nothing

julia> NamedArray([i > j ? cor(df[!, i], df[!, j]) : NoPrint() for i in 2:ncol(df), j in 1:ncol(df)-1],
                  (names(df)[2:end], names(df)[1:end-1]))
3Γ—3 Named Array{Any,2}
A β•² B β”‚        x1         x2         x3
──────┼────────────────────────────────
x2    β”‚ -0.555227                      
x3    β”‚ -0.102989  0.0988691           
x4    β”‚ -0.407655  0.0381506   0.444643

note again - that this assumes you do not need to do handling of missing values (as there are several strategies that could be used here).