-
-
Save viveksck/254794ad6f85e7bdd8b23f9b5bef3f51 to your computer and use it in GitHub Desktop.
Pandas recipe for better latex tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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