Last active
February 9, 2018 22:14
-
-
Save tbrittoborges/3f7f3f9c4bd0f797fb9a to your computer and use it in GitHub Desktop.
Pandas recipe for better latex tables
This file contains hidden or 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