# Get Correlation matrix from dataframe with row and column titles

**URL:** <https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [May 19, 2022, 7:04am UTC](https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301 "2022-05-19T07:04:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![DWSchulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dwschulze/32/35968_2.png) [@DWSchulze](https://discourse.julialang.org/u/DWSchulze)\
**Post date:** [May 19, 2022, 7:04am UTC](https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301/1 "2022-05-19T07:04:20Z")

</div>

I have a dataframe and I want to get the correlation matrix with row/column titles so I know what I’m looking at. If I do this

`cormat = cor(Matrix(df))`

There are no row/column titles. How do I get a correlation matrix / dataframe with row and column titles?

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [May 19, 2022, 7:24am UTC](https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301/2 "2022-05-19T07:24:23Z")

</div>

You can use `NamedArrays.jl` for labeled axes.

```julia
julia> na = NamedArray(Matrix(df))
5×3 Named Matrix{Float64}
A ╲ B │ 1 2 3
──────┼────────────────────────────────
1 │ 0.887614 0.695876 0.0307668
2 │ 0.133595 0.428006 0.400858
3 │ 0.149537 0.868851 0.612551
4 │ 0.179476 0.371094 0.976713
5 │ 0.565508 0.990754 0.0458062

julia> setnames!(na, names(df), 2)
(OrderedCollections.OrderedDict{Any, Int64}("1" => 1, "2" => 2, "3" => 3, "4" => 4, "5" => 5), OrderedCollections.OrderedDict{Any, Int64}("x" => 1, "y" => 2, "z" => 3))

julia> na
5×3 Named Matrix{Float64}
A ╲ B │ x y z
──────┼────────────────────────────────
1 │ 0.887614 0.695876 0.0307668
2 │ 0.133595 0.428006 0.400858
3 │ 0.149537 0.868851 0.612551
4 │ 0.179476 0.371094 0.976713
5 │ 0.565508 0.990754 0.0458062

julia> cor(na)
3×3 Named Matrix{Float64}
B ╲ B │ x y z
──────┼────────────────────────────────
x │ 1.0 0.406237 -0.783476
y │ 0.406237 1.0 -0.588138
z │ -0.783476 -0.588138 1.0

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 19, 2022, 8:50am UTC](https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301/3 "2022-05-19T08:50:43Z")

</div>

Would something like this help?

```julia
using DataFrames, Statistics
cols = [:x, :y, :z]
df = DataFrame(rand(5,3), cols)

dfcor = [cols DataFrame(cor(Matrix(df)), cols)]

 Row │ x1 x y z        
     │ Symbol Float64 Float64 Float64
─────┼──────────────────────────────────────────
   1 │ x 1.0 -0.0635502 0.252657
   2 │ y -0.0635502 1.0 0.370023
   3 │ z 0.252657 0.370023 1.0

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 19, 2022, 9:11am UTC](https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301/4 "2022-05-19T09:11:52Z")

</div>

The code below is not super efficient, but I often use it:

```julia
NamedArray(cor(Matrix(df)), (names(df), names(df)))

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [May 19, 2022, 12:28pm UTC](https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301/5 "2022-05-19T12:28:06Z")

</div>

I personally prefer AxisArrays.jl:

> **[GitHub - JuliaArrays/AxisArrays.jl: Performant arrays where each dimension...](https://github.com/JuliaArrays/AxisArrays.jl)**
>
> Performant arrays where each dimension can have a named axis with values - GitHub - JuliaArrays/AxisArrays.jl: Performant arrays where each dimension can have a named axis with values

```julia
julia> C = AxisArray(rand(3,3), row=[:a,:b,:c], col=[:a,:b,:c])
2-dimensional AxisArray{Float64,2,...} with axes:
    :row, [:a, :b, :c]
    :col, [:a, :b, :c]
And data, a 3×3 Matrix{Float64}:
 0.0344525 0.210511 0.365664
 0.270243 0.319163 0.113199
 0.960284 0.832544 0.105832

julia> C[:a,:b]
0.21051123926125237

julia> C[1,2]
0.21051123926125237

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 19, 2022, 12:31pm UTC](https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301/6 "2022-05-19T12:31:02Z")

</div>

This is something that should honestly be made easier. Perhaps a DataFrameStats.jl convenience package where operations like this are already implemented behind the scenes. I always run into this pain.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 19, 2022, 12:33pm UTC](https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301/7 "2022-05-19T12:33:23Z")

</div>

The printing could definitely be improved here though. The `NamedArrays.jl` version is easier to read because it lists the column and row names directly.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [May 19, 2022, 12:49pm UTC](https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301/8 "2022-05-19T12:49:53Z")

</div>

I would make it TableStats.jl and would try to support any Tables.jl though. A set of basic convenience functions that generalize cov, cor, mean, kurtosis, etc from Statistics and StatsBase.jl to work with table types.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 19, 2022, 5:09pm UTC](https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301/9 "2022-05-19T17:09:33Z")

</div>

Taking some arguments from a discussion on _correlated_ 😉 issues

```julia
julia> using DataFrames, Statistics

julia> cols = [:x, :y, :z]
3-element Vector{Symbol}:
 :x
 :y
 :z

julia> df = DataFrame(rand(5,3), cols)
5×3 DataFrame
 Row │ x y z        
     │ Float64 Float64 Float64
─────┼──────────────────────────────
   1 │ 0.234054 0.613994 0.557298
   2 │ 0.318559 0.748663 0.322644
   3 │ 0.999855 0.167148 0.801001
   4 │ 0.998599 0.549592 0.291748
   5 │ 0.932561 0.72125 0.688496

julia> corr=(;zip(cols,Tables.rowtable(DataFrame(cor(Matrix(df)),cols)))...)
(x = (x = 1.0, y = -0.4998593209925881, z = 0.32929593442917343), y = (x = -0.4998593209925881, y = 1.0, z = -0.5560163100630737), z = (x = 0.32929593442917343, y = -0.5560163100630737, z = 1.0))

julia> corr[:x][:x]
1.0

julia> corr[:z][:x]==corr[:x][:z]
true

```

---

<div class="post-metadata">

**Author:** ![DWSchulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dwschulze/32/35968_2.png) [@DWSchulze](https://discourse.julialang.org/u/DWSchulze)\
**Post date:** [May 19, 2022, 5:16pm UTC](https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301/10 "2022-05-19T17:16:13Z")

</div>

> [@skleinbo](#):
>
> `setnames!(na, names(df), 2)`

That works nicely. Thanks.

---

<div class="post-metadata">

**Author:** ![fdekerme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fdekerme/32/43574_2.png) [@fdekerme](https://discourse.julialang.org/u/fdekerme)\
**Post date:** [February 29, 2024, 2:21pm UTC](https://discourse.julialang.org/t/get-correlation-matrix-from-dataframe-with-row-and-column-titles/81301/11 "2024-02-29T14:21:49Z")

</div>

Hello, do you know if there have been any developments on these aspects since the original post? Any new packages to do that smoothly with Dataframes ?  
Thank you!
