gpt4 book ai didi

python - 使用 Pandas Dataframe.to_latex() 格式化表格

转载 作者:行者123 更新时间:2023-12-04 17:07:56 25 4
gpt4 key购买 nike

有什么方法可以指导 Pandas Dataframe.to_latex()追加 \footnotesize (或其他全局选项)用于 LateX 中的输出表? (当然,除了手动附加它,效率不高,因为我要生成很多表。)
所以,现在我的代码生成以下 LaTeX 表:

\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}
来自以下 Pandas 数据框:
                     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
以及用于可重复性目的的相应字典:
{'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() .
我想要的是更改一些全局表选项,例如添加\footnotesize,更改\centering,如下所示:
\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}
乍一看, Dataframe.to_latex() 中似乎没有这样的选项。 ,但我不确定 formatters 字段可以做什么。此外,我不知道 Styler.to_latex() 是否可以在这里提供帮助。
PS :一个额外的好处是定义在哪些数据集(例如 S)之后包含一个\midrule(因为我想分离不同类型的数据集)。
\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}

最佳答案

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

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

关于python - 使用 Pandas Dataframe.to_latex() 格式化表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70127250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com