Skip to content

Instantly share code, notes, and snippets.

@viveksck
Forked from tbrittoborges/pd_latex_table.py
Created February 9, 2018 22:14
Show Gist options
  • Save viveksck/254794ad6f85e7bdd8b23f9b5bef3f51 to your computer and use it in GitHub Desktop.
Save viveksck/254794ad6f85e7bdd8b23f9b5bef3f51 to your computer and use it in GitHub Desktop.
Pandas recipe for better latex tables
def better_table(table, caption, name):
start = r"""
\begin{{table}}[!htb]
\sisetup{{round-mode=places, round-precision=2}}
\caption{{{}}}\label{{table:{}}}
\centering
""".format(caption, name)
end = r"\end{table}"
numerical_format = r'\num{{{}}}'.format
table_latex = table.to_latex(
escape=False,
column_format="@{{}} *{0}l @{{}}".format(table.shape[1] + 1),
formatters={k: numerical_format for k in table.columns[table.dtypes != 'object']})
return start + table_latex + end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment