2021-11-27

Table formatting with pandas Dataframe.to_latex()

Is there any way to instruct pandas Dataframe.to_latex() to append \footnotesize (or other global options) for the output table in LateX? (of course, other than manually append it ;-), which is not efficient, as I'm generating lot's of tables)

So, right now my code produces the following LateX table:

\begin{table}[H]
\centering
\caption{Caption of the table.}
\label{tab:06_01.example}
\begin{tabular}{lrrr}
\toprule
                    &            &  F-1 &    F-2 \\
Dataset & Model &            &       \\
\midrule
\multirow{2}{*}{\textit{H}} & Baseline &      0.904 & 0.887 \\
                    & Version2 &      0.939 & 0.927 \\
\cline{1-4}
\multirow{2}{*}{\textit{S}} & Baseline &      0.548 & 0.506 \\
                    & Version2 &      0.582 & 0.541 \\
\cline{1-4}
\midrule
\multirow{2}{*}{\textit{G}} & Baseline &      0.879 & 0.855 \\
                    & Version2 &      0.910 & 0.895 \\
\cline{1-4}
\multirow{2}{*}{\textit{T}} & Baseline &      0.911 & 0.877 \\
                    & Version2 &      0.940 & 0.913 \\
\bottomrule
\end{tabular}
\end{table}

from the following pandas dataframe:

                     F-1   F-2
dataset Model                 
H       Baseline   0.904 0.887
        Version2   0.939 0.927
S       Baseline   0.548 0.506
        Version2   0.582 0.541
G       Baseline   0.879 0.855
        Version2   0.910 0.895
T       Baseline   0.911 0.877
        Version2   0.940 0.913

and the corresponding dict for reproducibility purposes:

{'F-1': {('H', 'Baseline'): 0.9044961552465764, ('H', 'Fine-Tuned'): 0.9387767951280728, ('S', 'Baseline'): 0.547968262581014, ('S', 'Fine-Tuned'): 0.5815634664656218, ('G', 'Baseline'): 0.8793941208568047, ('G', 'Fine-Tuned'): 0.9102870296052078, ('T', 'Baseline'): 0.9110316123313993, ('T', 'Fine-Tuned'): 0.9404444309041384}, 'F-2': {('H', 'Baseline'): 0.8865304318012182, ('H', 'Fine-Tuned'): 0.9273671656403047, ('S', 'Baseline'): 0.5063582247873787, ('S', 'Fine-Tuned'): 0.5408162758046822, ('G', 'Baseline'): 0.8551648617281388, ('G', 'Fine-Tuned'): 0.8947135188980437, ('T', 'Baseline'): 0.8774834363467384, ('T', 'Fine-Tuned'): 0.9134634736945935}}

through an almost direct dataframe.to_latex(). What I would like is to change some global table options, e.g. add \footnotesize, change \centering, like this:

\begin{table}[H]
\footnotesize %include or not
\centering %include or not
\caption{Caption of the table.}
\label{tab:06_01.example}
\begin{tabular}{lrrr}
\toprule
                    &            &  F-1 &    F-2 \\
Dataset & Model &            &       \\
\midrule
\multirow{2}{*}{\textit{H}} & Baseline &      0.904 & 0.887 \\
                    & Version2 &      0.939 & 0.927 \\
\cline{1-4}
\multirow{2}{*}{\textit{S}} & Baseline &      0.548 & 0.506 \\
                    & Version2 &      0.582 & 0.541 \\
\cline{1-4}
\multirow{2}{*}{\textit{G}} & Baseline &      0.879 & 0.855 \\
                    & Version2 &      0.910 & 0.895 \\
\cline{1-4}
\multirow{2}{*}{\textit{T}} & Baseline &      0.911 & 0.877 \\
                    & Version2 &      0.940 & 0.913 \\
\bottomrule
\end{tabular}
\end{table}

At 1st sight, it seems that there isn't such an option within Dataframe.to_latex(), but I'm not sure about what formatters field can do. Furthermore, I don't know if Styler.to_latex() could help here.

PS: An additional nice-to-have would be to define after which datasets (e.g. S) to include a \midrule (as I would like to separate different types of datasets).

\begin{table}[H]
\footnotesize %include or not
\centering %include or not
\caption{Caption of the table.}
\label{tab:06_01.example}
\begin{tabular}{lrrr}
\toprule
                    &            &  F-1 &    F-2 \\
Dataset & Model &            &       \\
\midrule
\multirow{2}{*}{\textit{H}} & Baseline &      0.904 & 0.887 \\
                    & Version2 &      0.939 & 0.927 \\
\cline{1-4}
\multirow{2}{*}{\textit{S}} & Baseline &      0.548 & 0.506 \\
                    & Version2 &      0.582 & 0.541 \\
\cline{1-4}
\midrule
\multirow{2}{*}{\textit{G}} & Baseline &      0.879 & 0.855 \\
                    & Version2 &      0.910 & 0.895 \\
\cline{1-4}
\multirow{2}{*}{\textit{T}} & Baseline &      0.911 & 0.877 \\
                    & Version2 &      0.940 & 0.913 \\
\bottomrule
\end{tabular}
\end{table}


from Recent Questions - Stack Overflow https://ift.tt/3FN9bMA
https://ift.tt/eA8V8J

No comments:

Post a Comment