tompollard / tableone

Create "Table 1" for research papers in Python
https://pypi.python.org/pypi/tableone/
MIT License
159 stars 38 forks source link

DataFrame.to_latex warning #140

Closed welyt closed 1 year ago

welyt commented 1 year ago

in future versions DataFrame.to_latex is expected to utilise the base implementation of Styler.to_latex for formatting and rendering. The arguments signature may therefore change. It is recommended instead to use DataFrame.style.to_latex which also contains additional functionality.

tompollard commented 1 year ago

It looks like the fix for this is to add Jinja2 as a dependency. A couple of notes on using to_latex:

  1. it is important to pass the escape=True argument, otherwise tables will not render correctly.
  2. \usepackage{{booktabs}} needs to be included in the latex document.

A minimal example of using to_latex() is below:

from tableone import TableOne, load_dataset

data = load_dataset('pn2012')

table1 = TableOne(data, dip_test=True, normal_test=True, tukey_test=True, groupby="death")

print(table1.to_latex(escape=True))

...which outputs:

\begin{tabular}{llll}
\toprule
 &  & Missing & Overall \\
\midrule
n &  &  & 1000 \\
\cline{1-4}
Age, mean (SD) &  & 0 & 65.0 (17.2) \\
\cline{1-4}
SysABP, mean (SD) &  & 291 & 114.3 (40.2) \\
\cline{1-4}
Height, mean (SD) &  & 475 & 170.1 (22.1) \\
\cline{1-4}
Weight, mean (SD) &  & 302 & 82.9 (23.8) \\
\cline{1-4}
\multirow[t]{4}{*}{ICU, n (\%)} & CCU & 0 & 162 (16.2) \\
 & CSRU &  & 202 (20.2) \\
 & MICU &  & 380 (38.0) \\
 & SICU &  & 256 (25.6) \\
\cline{1-4}
\multirow[t]{2}{*}{MechVent, n (\%)} & 0 & 0 & 540 (54.0) \\
 & 1 &  & 460 (46.0) \\
\cline{1-4}
LOS, mean (SD) &  & 0 & 14.2 (14.2) \\
\cline{1-4}
\multirow[t]{2}{*}{death, n (\%)} & 0 & 0 & 864 (86.4) \\
 & 1 &  & 136 (13.6) \\
\cline{1-4}
\bottomrule
\end{tabular}

This can be pasted into a LaTeX document like the one below (for example, in https://www.overleaf.com/):

\documentclass{article}
\usepackage{multirow}
\usepackage{{booktabs}}

\begin{document}

\begin{table}[htbp]
\centering

\begin{tabular}{llllll}
\toprule
 &  & \multicolumn{4}{r}{Grouped by death} \\
 &  & Missing & Overall & 0 & 1 \\
\midrule
n &  &  & 1000 & 864 & 136 \\
Age, mean (SD) &  & 0 & 65.0 (17.2) & 64.0 (17.4) & 71.7 (14.0) \\
SysABP, mean (SD) &  & 291 & 114.3 (40.2) & 115.4 (38.3) & 107.6 (49.4) \\
Height, mean (SD) &  & 475 & 170.1 (22.1) & 170.3 (23.2) & 168.5 (11.3) \\
Weight, mean (SD) &  & 302 & 82.9 (23.8) & 83.0 (23.6) & 82.3 (25.4) \\
\multirow[t]{4}{*}{ICU, n (\%)} & CCU & 0 & 162 (16.2) & 137 (15.9) & 25 (18.4) \\
 & CSRU &  & 202 (20.2) & 194 (22.5) & 8 (5.9) \\
 & MICU &  & 380 (38.0) & 318 (36.8) & 62 (45.6) \\
 & SICU &  & 256 (25.6) & 215 (24.9) & 41 (30.1) \\
\multirow[t]{2}{*}{MechVent, n (\%)} & 0 & 0 & 540 (54.0) & 468 (54.2) & 72 (52.9) \\
 & 1 &  & 460 (46.0) & 396 (45.8) & 64 (47.1) \\
LOS, mean (SD) &  & 0 & 14.2 (14.2) & 14.0 (13.5) & 15.4 (17.7) \\
\bottomrule
\end{tabular}

\caption{A test caption}
\label{table2}
\end{table}

\end{document}

...which renders a table like this:

Screenshot 2023-04-23 at 8 42 46 PM