I know about PrettyTables, but how do I save to file, letβs say table_xy.md
, a DataFrame ?
This works, but it also prints the types:
using DataFrames, PrettyTables
# Create or load your DataFrame
df = DataFrame(A = [1, 2, 3], B = ["x", "y", "z"], C = [1.1, 2.2, 3.3])
# Save as markdown table to file
open("table.md", "w") do io
pretty_table(io, df, backend = :markdown)
end
Thereβs kwarg to supress the subheader β¦ show_subheader = false
hmm.. I got an error that that keyword is not supported by the pretty_table
function, and itβs not in its docstring. I am in PrettyTables.jl 3.0.10 that is the latest releaseβ¦
df = DataFrame(A = [1, 2, 3], B = ["x", "y", "z"], C = [1.1, 2.2, 3.3])
# Save as markdown table to file
open("table.md", "w") do io pretty_table(io, df, backend = Val(:markdown), show_subheader = false)
3Γ3 DataFrame
Row β A B C
β Int64 String Float64
ββββββΌββββββββββββββββββββββββ
1 β 1 x 1.1
2 β 2 y 2.2
3 β 3 z 3.3
A | B | C |
---|---|---|
1 | x | 1.1 |
2 | y | 2.2 |
3 | z | 3.3 |
Julia v1.11:
julia> using Pkg
julia> Pkg.activate(;temp=true)
Activating new project at `/tmp/jl_o6uGoc`
julia> Pkg.add(["PrettyTables","DataFrames"])
Resolving package versions...
Updating `/tmp/jl_o6uGoc/Project.toml`
[a93c6f00] + DataFrames v1.8.0
[08abe8d2] + PrettyTables v3.0.10
Updating `/tmp/jl_o6uGoc/Manifest.toml`
[34da2185] + Compat v4.18.0
[a8cc5b0e] + Crayons v4.1.1
[9a962f9c] + DataAPI v1.16.0
[a93c6f00] + DataFrames v1.8.0
[864edb3b] + DataStructures v0.19.1
[e2d170a0] + DataValueInterfaces v1.0.0
[842dd82b] + InlineStrings v1.4.5
[41ab1584] + InvertedIndices v1.3.1
[82899510] + IteratorInterfaceExtensions v1.0.0
[b964fa9f] + LaTeXStrings v1.4.0
[e1d29d7a] + Missings v1.2.0
[bac558e1] + OrderedCollections v1.8.1
[2dfb63ee] + PooledArrays v1.4.3
β
[aea7be01] + PrecompileTools v1.2.1
[21216c6a] + Preferences v1.5.0
[08abe8d2] + PrettyTables v3.0.10
[189a3867] + Reexport v1.2.2
[91c51154] + SentinelArrays v1.4.8
[a2af1166] + SortingAlgorithms v1.2.2
[10745b16] + Statistics v1.11.1
[892a3eda] + StringManipulation v0.4.1
[3783bdb8] + TableTraits v1.0.1
[bd369af6] + Tables v1.12.1
[56f22d72] + Artifacts v1.11.0
[2a0f44e3] + Base64 v1.11.0
[ade2ca70] + Dates v1.11.0
[9fa8497b] + Future v1.11.0
[b77e0a4c] + InteractiveUtils v1.11.0
[8f399da3] + Libdl v1.11.0
[37e2e46d] + LinearAlgebra v1.11.0
[d6f4376e] + Markdown v1.11.0
[de0858da] + Printf v1.11.0
[3fa0cd96] + REPL v1.11.0
[9a3f8284] + Random v1.11.0
[ea8e919c] + SHA v0.7.0
[6462fe0b] + Sockets v1.11.0
[f489334b] + StyledStrings v1.11.0
[fa267f1f] + TOML v1.0.3
[cf7118a7] + UUIDs v1.11.0
[4ec0a83e] + Unicode v1.11.0
[e66e0078] + CompilerSupportLibraries_jll v1.1.1+0
[4536629a] + OpenBLAS_jll v0.3.27+1
[8e850b90] + libblastrampoline_jll v5.11.0+0
Info Packages marked with β
have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated -m`
Precompiling project...
1 dependency successfully precompiled in 28 seconds. 35 already precompiled.
julia> using PrettyTables, DataFrames
julia> df = DataFrame(A = [1, 2, 3], B = ["x", "y", "z"], C = [1.1, 2.2, 3.3])
3Γ3 DataFrame
Row β A B C
β Int64 String Float64
ββββββΌββββββββββββββββββββββββ
1 β 1 x 1.1
2 β 2 y 2.2
3 β 3 z 3.3
julia> open("table.md", "w") do io pretty_table(io, df, backend = Val(:markdown), show_subhead = false) end
ERROR: TypeError: in keyword argument backend, expected Symbol, got a value of type Val{:markdown}
Stacktrace:
[1] (::var"#11#12")(io::IOStream)
@ Main ./REPL[6]:1
[2] open(::var"#11#12", ::String, ::Vararg{String}; kwargs::@Kwargs{})
@ Base ./io.jl:410
[3] open(::Function, ::String, ::String)
@ Base ./io.jl:407
[4] top-level scope
@ REPL[6]:1
This will not work, just write:
:markdown
instead.
Hi!
Notice that show_subheader
is from the old API (PrettyTables v2). DataFrames.jl 1.8 now uses PrettyTables.jl v3. Hence, this will not work anymore. You can achieve the same behavior by passing show_first_column_label_only = true
.
Oof, show_first_column_label_only
is way harder to remember than show_subheader
.
Could you make show_column_labels::Union{Bool, Symbol} = :first
instead?
Hi @Nathan_Boyer!
No, it will be a breaking change unless we have two keywords to do the same thing, which is very confusing.
You could leave the old way working but deprecate it and remove it from the documentation?
Hum, also no. PrettyTables.jl v3 was a huge rewrite. I do not see any gain by allowing just this specific keyword from the old API.
I thought show_column_labels
is part of the new API? It is listed in the dev documentation. My proposal is just to allow this keyword to take other values like :first
(or maybe 1
). I think that would be more ergonomic than the new separate show_first_column_label_only
keyword.
Just a suggestion. Iβve highjacked this thread long enough.