问题:Pandas.DataFrame.to_latex()输出的表不包含长列标题。从Pandas 0.24开始,删除了line_width =参数。
讨论:
我正在使用df.to_latex()命令导出许多表,以包含在主文档中。许多表的标题栏比较罗word,主要是因为需要包括括号内的单元。
最终结果是一组荒谬的稀疏表,这些表通常不适合页面显示。
下面是生成的稀疏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}
问题:
\input
这些表并指定包裹宽度?答案 0 :(得分:0)
问题取决于列的声明方式。指定“ r”时,您说“对足够大的单元格进行一列足够宽并右对齐”。
您需要做的是给列的宽度设置为p {width},在这种情况下,列项目将被格式化为给定宽度的段落,并根据需要使用换行符。
\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}