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]))
using Random
using DataFrames
using PrettyTables
using Formatting
function formatters(dataframe, formatters...)
_dataframe = dataframe
_formatters = Dict(formatters)
return function (v, i, j)
column = propertynames(_dataframe)[j]
if haskey(_formatters, column)
return sprintf1(_formatters[column], v)
end
v
end
end
df = DataFrame(rand(5, 3), :auto)
pretty_table(df, formatters = formatters(df, :x1 => "%.1f", :x2 => "%.2f", :x3 => "%.3f"))
Natively it is not possible because PrettyTables does not “understand” the column names as in DataFrames. I think the best approach is the one proposed by @rafael.guerra
I will use the closure approach (post #2) because it keeps the column and format tied together, do not rely on order and I find more ergonomic to type the column symbol than the quoted raw string. Also it allows to layout the code in rows which is nice to comment and uncomment specific columns.
Do you know if there is a way of suppressing the subheader (data types) from printing? Same data types such as CategoricalValues tends to be long and take up too much space.