I'm producing several LateX tables through python/pandas.
Up until recently, the tables' headers were enclosed in {}
, without any explicit instruction for that, ie:
\begin{tabular}{llrrr}
{} & {date} & {x} & {y} & {z} \\
{} & {(yyyy-mm-dd)} & {(s)} & {(m)} & {(kg)} \\
0 & 2014-03-12 & 1 & 2 & 3 \\
1 & 2014-03-13 & 4 & 5 & 6 \\
\end{tabular}
But recently, the output from pandas.style.to_latex() started being the following (without the {}
):
\begin{tabular}{llrrr}
& date & x & y & z \\
& (yyyy-mm-dd) & (s) & (m) & (kg) \\
0 & 2014-03-12 & 1 & 2 & 3 \\
1 & 2014-03-13 & 4 & 5 & 6 \\
\end{tabular}
As I'm post-processing these LateX tables in python, I need the headers to be enclosed in {}
Does anyone know what I must do for that?
The original pandas dataframe can be generated through:
columns = pd.MultiIndex.from_tuples(
zip(['date', 'x', 'y', 'z'],
['(yyyy-mm-dd)', '(s)', '(m)', '(kg)']))
data = [['2014-03-12', 1, 2, 3],
['2014-03-13', 4, 5, 6]]
df = pd.DataFrame(data, columns=columns)
CodePudding user response:
two ways,
df.style.to_latex(siunitx=True)
(since siunitx package requires this formatting)
or
df.style.format_index("{{{0}}}", axis=1).to_latex()
(explicitly using the format function to control the output)