Making Multi-row work in pandas dataframe.to_latex()

Viewed 466

I have the following dataframe:

      dataset Baseline       Version2      
               F-1   F-2      F-1   F-2
0       G    0.971 0.967    0.971 0.967
1       H    0.780 0.762    0.800 0.777
2       S    0.401 0.320    0.453 0.365
3       T    0.881 0.825    0.989 0.985

My goal is to export this to LaTeX, which I do by pandas.DataFrame.to_latex():

latex = df.to_latex(
        index=False,
        escape=False,
        sparsify=True,
        multirow=True,
        multicolumn=True,
        multicolumn_format='c',
        position='H'
    )

obtaining the following code in LaTeX:

\begin{table}[H]
\centering
\begin{tabular}{lrrrr}
\toprule
dataset & \multicolumn{2}{c}{Baseline} & \multicolumn{2}{c}{Version2} \\
        &      F-1 &   F-2 &      F-1 &   F-2 \\
\midrule
      G &    0.971 & 0.967 &    0.971 & 0.967 \\
      H &    0.780 & 0.762 &    0.800 & 0.777 \\
      S &    0.401 & 0.320 &    0.453 & 0.365 \\
      T &    0.881 & 0.825 &    0.989 & 0.985 \\
\bottomrule
\end{tabular}
\end{table}

which is rendered like this: enter image description here My issue is: I'd expect a multirow to appear in the "dataset", covering the 2 lines with "dataset" and "", like this: enter image description here I have multirow=True in the options, but I can't make this work. Any idea on how can I fix this?

0 Answers
Related