3
votes

Problem: Tables output by Pandas.DataFrame.to_latex() do not wrap long column headers. The line_width= parameter has been removed as of Pandas 0.24.

Discussion:

I'm exporting many tables with the df.to_latex() command, for inclusion in a master document. Many of the tables have wordy column headers, mostly owing to the need to include parenthesized units.

The end result is a set of absurdly sparse tables that often won't fit on the page.

Here's a sample of the too-sparse generated 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}

Questions:

  • Is there an alternate method of specifying wrapping in my generated .tex?
  • Alternately, is there latex magic that will let my master latex document \input these tables and specify a wrap width?
1

1 Answers

4
votes

The problem relies on the way columns are declared. When specifying 'r', you say 'do a column wide enough for the larger cell and right aligned'.

What you need to do is use the column_format argument to give a width of the column with p{width}, in which case columns items will be formatted as a paragraph of the given width with line breaking as required.

E.g., the Pandas command:

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

Should result in an output .tex file that looks more-or-less like this:

\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