Formatting DataFrames with PrettyTables

alexgr
Fri, 30 Sep 2022 11:57:58 +0000
General Usage

I’d like to format some numeric columns with different decimal positions. The approach I am following is this:

using Random
using DataFrames
using PrettyTables

df = DataFrame(rand(5, 3), :auto)
pretty_table(df, formatters = ft_printf(["%.1f", "%.2f", "%.3f"], [1, 2, 3]))

┌─────────┬─────────┬─────────┐
│      x1 │      x2 │      x3 │
│ Float64 │ Float64 │ Float64 │
├─────────┼─────────┼─────────┤
│     0.1 │    0.78 │   0.750 │
│     0.2 │    0.69 │   0.366 │
│     0.4 │    0.15 │   0.127 │
│     0.4 │    0.84 │   0.096 │
│     0.1 │    0.88 │   0.107 │
└─────────┴─────────┴─────────┘

How would you go about specifying the format by column symbols instead of the position?
Something like…

pretty_table(df, formatters = Dict(:x1 => "%.1f", :x2 => "%.2f", :x3 =>"%.3f"))

10 posts - 4 participants