pandas - 将列标题包装在 pandas.df.to_latex() 导出的 latex 表中

标签 pandas latex

问题 Pandas.DataFrame.to_latex() 输出的表不包含长列标题。从 Pandas 0.24 开始,line_width= 参数已被删除。

讨论:

我正在使用 df.to_latex() 命令导出许多表,以包含在主文档中。许多表格都有冗长的列标题,主要是因为需要包含带括号的单位。

最终结果是一组稀疏得荒谬的表格,通常页面上放不下。

这是生成的太稀疏的 Tex 的示例:

\begin{tabular}{lrrrrrr}
\toprule
{} & Odometer (km/y) & Fuel (L/y) & Elec (kWh/y) & Economy (L/100km) & GHG (kg CO2e) & GHG (g/km) \\
Type          &                 &            &              &                   &               &            \\
\midrule
Type 1         & 70753.62 & 9721.06 & 0.00 & 13.74 & 21386.33 & 302.26 \\
Type RRR     & 56167.39 & 5285.40 & 1627.60 & 9.41 & 11642.54 & 207.28 \\
Type X        & 195756.35 & 42957.04 & 0.00 & 21.94 & 94505.48 & 482.77 \\
Type Huh        & 187384.66 & 18118.07 & 73.07 & 9.67 & 39860.40 & 212.72 \\
\bottomrule
\end{tabular}

问题:

  • 在我生成的 .tex 文件中是否有指定换行的替代方法?
  • 或者,是否有 latex 魔法可以让我的 master latex 记录 \input 这些表格并指定换行宽度?

最佳答案

问题取决于声明列的方式。指定“r”时,您说“为较大的单元格做一个足够宽的列并右对齐”。

您需要做的是使用 column_format 参数给列的宽度 p{width},在这种情况下,列项目将被格式化为给定宽度的段落根据需要打破。

例如,Pandas 命令:

df.to_latex(column_format='lp{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}')

应该产生一个看起来或多或少像这样的输出 .tex 文件:

\documentclass{article}

\begin{document}
\begin{tabular}{lp{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}}
\hline
  {} & Odometer (km/y) & Fuel (L/y) & Elec (kWh/y) & Economy (L/100km) & GHG (kg CO2e) & GHG (g/km) \\
  Type          &                 &            &              &                   &               &            \\
  \hline
  Type 1         & 70753.62 & 9721.06 & 0.00 & 13.74 & 21386.33 & 302.26 \\
  Type RRR     & 56167.39 & 5285.40 & 1627.60 & 9.41 & 11642.54 & 207.28 \\
  Type X        & 195756.35 & 42957.04 & 0.00 & 21.94 & 94505.48 & 482.77 \\
  Type Huh        & 187384.66 & 18118.07 & 73.07 & 9.67 & 39860.40 & 212.72 \\
  \hline
\end{tabular}
\bigskip

If you want to control line breaking, just add a $\backslash${newline} where you want.

\begin{tabular}{lp{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}}
\hline
  {} & Odometer\newline (km/y) & Fuel \newline (L/y) & Elec\newline (kWh/y) & Economy\newline (L/100km) & GHG\newline (kg CO2e) & GHG\newline (g/km) \\
  Type          &                 &            &              &                   &               &            \\
  \hline
  Type 1         & 70753.62 & 9721.06 & 0.00 & 13.74 & 21386.33 & 302.26 \\
  Type RRR     & 56167.39 & 5285.40 & 1627.60 & 9.41 & 11642.54 & 207.28 \\
  Type X        & 195756.35 & 42957.04 & 0.00 & 21.94 & 94505.48 & 482.77 \\
  Type Huh        & 187384.66 & 18118.07 & 73.07 & 9.67 & 39860.40 & 212.72 \\
  \hline
\end{tabular}
\bigskip

You can also consider adding an extra row for the units.

\begin{tabular}{lp{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}}
\hline
  {} & Odometer & Fuel  & Elec & Economy & GHG & GHG \\
  Type &  (km/y) &  (L/y) & (kWh/y) & (L/100km) &  (kg CO2e) &  (g/km) \\
  \hline
  Type 1         & 70753.62 & 9721.06 & 0.00 & 13.74 & 21386.33 & 302.26 \\
  Type RRR     & 56167.39 & 5285.40 & 1627.60 & 9.41 & 11642.54 & 207.28 \\
  Type X        & 195756.35 & 42957.04 & 0.00 & 21.94 & 94505.48 & 482.77 \\
  Type Huh        & 187384.66 & 18118.07 & 73.07 & 9.67 & 39860.40 & 212.72 \\
  \hline
\end{tabular}

\end{document}

enter image description here

关于pandas - 将列标题包装在 pandas.df.to_latex() 导出的 latex 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56434118/

相关文章:

python - matplotlib 中的中心线断轴标签

formatting - 引用格式和 hyperref 包

r - RMarkdown 演示文稿中的列中的代码

python - 为什么我使用多处理和 pandas 会收到此 KeyError ?

pandas - 根据系列和数据帧中的内容生成 boolean 数据帧

python - 在Python中合并两个数据框

python - 复杂的 pandas 子设置;选择与多列中的条件匹配的行

c# - 如何将 LateX/Math 添加到 Windows 10 Ink API?

python - Pandas 组数据框直到特定值