Formatting DataFrames with PrettyTables

Alrigth. The same approach could be adapted to your use case:

using Random, DataFrames, PrettyTables

df = DataFrame(rand(5, 3), [:x3, :x1, :x2])

function formatters(df, fmt...)
    d = Dict(Symbol.(names(df)) .=> 1:ncol(df))
    keys = [first.(fmt)...]
    values = [last.(fmt)...]
    ft_printf(values, [d[s] for s in keys])
end

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

pretty_table(df, formatters = fmt)

┌─────────┬─────────┬─────────┐
│      x3 │      x1 │      x2 │
│ Float64 │ Float64 │ Float64 │
├─────────┼─────────┼─────────┤
│   0.209 │     0.4 │    0.58 │
│   0.102 │     1.0 │    0.60 │
│   0.454 │     0.9 │    0.83 │
│   0.135 │     0.9 │    0.48 │
│   0.063 │     0.4 │    0.99 │
└─────────┴─────────┴─────────┘
1 Like