首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用熊猫Dataframe.to_latex()格式化表

用熊猫Dataframe.to_latex()格式化表
EN

Stack Overflow用户
提问于 2021-11-26 16:13:34
回答 1查看 693关注 0票数 3

有没有办法指示熊猫Dataframe.to_latex()为LateX中的输出表附加\footnotesize (或其他全球选项)?(当然,除了手动添加之外,这是没有效率的,因为我正在生成许多表。)

因此,现在我的代码生成以下LaTeX表:

代码语言:javascript
运行
复制
\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}

来自下列熊猫的资料:

代码语言:javascript
运行
复制
                     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

以及相应的可重现性的dict:

代码语言:javascript
运行
复制
{'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}}

通过一个几乎直接的dataframe.to_latex()。我想要的是更改一些全局表选项,例如,添加\脚注大小,更改\居中,如下所示:

代码语言:javascript
运行
复制
\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}

乍一看,乳胶()中似乎没有这样的选项,但我不确定格式化器字段可以做什么。此外,我不知道Styler.to_latex()在这里是否有帮助。

PS:另一种好的方法是定义在哪个数据集(例如S)之后包含一个\中间规则(就像我想分离不同类型的数据集一样)。

代码语言:javascript
运行
复制
\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}
EN

回答 1

Stack Overflow用户

发布于 2021-11-26 17:41:17

您可以告诉latex对您的所有表进行这些更改:

代码语言:javascript
运行
复制
\documentclass{article}

\usepackage{float}
\usepackage{booktabs}
\usepackage{multirow}

% change fontsize
\AtBeginEnvironment{tabular}{\footnotesize} 

% switch off centering in tables
\AtBeginEnvironment{table}{\let\centering\relax}

\begin{document}

\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}

test

\centering


test

\end{document}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70127250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档