logo

📂Julia

How to View a Summary of a Data Frame in Julia

Guide1

When handling data frames in Julia, you can use the describe function to quickly check basic statistics. This plays a role similar to the summary function in R. As an example, suppose the following data frame is given.

julia> data = DataFrame(x = rand(10), y = 1 .+ randn(10), z = 10randn(10))
10×3 DataFrame
 Row │ x         y         z         
     │ Float64   Float64   Float64   
─────┼───────────────────────────────
   1 │ 0.581684  0.613567   -6.96196
   2 │ 0.167714  1.86029     2.25717
   3 │ 0.6753    0.397726   -6.88198
   4 │ 0.605366  1.27918    16.082
   5 │ 0.398977  0.655179   -3.7648
   6 │ 0.283162  1.40468    15.9616
   7 │ 0.366776  0.680256   11.8967
   8 │ 0.387233  2.15353    -3.79308
   9 │ 0.556446  1.58978   -11.0116
  10 │ 0.344502  0.894148   18.8104

Here, $X \sim U (0, 1)$, $Y \sim N(1, 1)$, and $Z \sim N(0, 10)$.

Basic Statistics

julia> describe(data)
3×7 DataFrame
 Row │ variable  mean      min         median     max       nmissing  eltype   
     │ Symbol    Float64   Float64     Float64    Float64   Int64     DataType 
─────┼─────────────────────────────────────────────────────────────────────────
   1 │ x         0.436716    0.167714   0.393105   0.6753          0  Float64
   2 │ y         1.15283     0.397726   1.08667    2.15353         0  Float64
   3 │ z         3.25945   -11.0116    -0.753812  18.8104          0  Float64

Using the describe function without any options lets you check, for each column, the mean, minimum, median, maximum, number of missing values, and data type, as shown above.

Specific Statistics

julia> describe(data, :mean, :std)
3×3 DataFrame
 Row │ variable  mean      std       
     │ Symbol    Float64   Float64   
─────┼───────────────────────────────
   1 │ x         0.436716   0.161219
   2 │ y         1.15283    0.593586
   3 │ z         3.25945   11.3199

You can also view only the statistics you want. The candidates provided by default are :mean, :std, :min, :q25, :median, :q75, :max, :sum, :eltype, :nunique, :nuniqueall, :first, :last, :nnonmissing, and :nmissing, and using :all lets you check every statistic listed.

User-Defined Statistics

julia> describe(data, rand => :rand)
3×2 DataFrame
 Row │ variable  rand      
     │ Symbol    Float64   
─────┼─────────────────────
   1 │ x          0.556446
   2 │ y          0.680256
   3 │ z         -3.79308

It does not necessarily have to be a statistic; it seems that any function that takes a vector and returns a scalar can be used. If you give a pair in the form function => :name, the function function is applied to every column and the result remains under the column name :name. In the example above, one value was randomly drawn from each column.

Complete Code

using DataFrames

data = DataFrame(x = rand(10), y = 1 .+ randn(10), z = 10randn(10))

describe(data)
describe(data, :mean, :std)
describe(data, rand => :rand)