'Pandas to Latex, formating floats in rows of data rather then per column

Ok here is my Problem:

I have this dictionary of data, which I did read in from an excel sheet. So far so good:

{'component 1': {
    'a': 0.6512361, 
    'b': 0.28123, 
    'c': 0.351282, 
    'd': 0.0 
    'e': 10
}, 'component 2': {
    'a': 0.2391361, 
    'b': 0.12933, 
    'c': 0.14982, 
    'd': 0.0 
    'e': 10
}

I would like to print this into a Latex file with the associated figures and some additional information. But I can't seem to find a way to style the resulting table to my liking.

Ideally it should look something like this:

\begin{table}[]
\begin{tabular}{lll}
  & component 1 & component 2  \\
a &  0.65       &  0.24        \\
b &  0.28       &  0.13        \\
c &  0.35       &  0.15        \\
d &  0.0        &  0.0         \\
e & 90          & 10         
\end{tabular}
\end{table}

The most important part of the table is the precision of the rows. I'm working with scientific data and the precision is essential to the data.

I get either:

Wrong Table 1:

\begin{table}[]
\begin{tabular}{lll}
  & component 1 & component 2  \\
a &  0.65       &  0.24        \\
b &  0.28       &  0.13        \\
c &  0.35       &  0.15        \\
d &  0.00       &  0.00        \\
e & 90.00       & 10.00         
\end{tabular}
\end{table}

which does not have the right formatting of for the floats (which is rather important since the precision is relevant to the data), or I get:

Wrong Table 2:


\begin{table}[]
\begin{tabular}{llllll}
            & a    & b    & c    & d   & e  \\
component 1 & 0.65 & 0.28 & 0.35 & 0.0 & 90 \\
component 2 & 0.24 & 0.13 & 0.15 & 0.0 & 10
\end{tabular}
\end{table}


which is transposed to what I want, but at least with the right formatting.

But I have not yet found a way to style the ROWS of the dataframe before it is printed to LateX.

latexDic = pandaDic.style\
    .format(
        subset=["$\lambda (mm/s)$", "$\Delta E_Q (mm/s)$" , "$\Gamma (mm/s)$"], 
        precision=2
        )\
    .format(
        subset=["Area (\%)"],
        precision=0
    )\
    .format(
        subset=["$\eta$"],
        precision=1
    )\
    .to_latex(
        buf="test.tex",
        caption="This is a Test",
    )

With this I can set up the table just fine, but it is transposed (see table 2) to what I want from the final TeX.

I have found nothing to reference rows in df.style.format().

I also tried to setup the formatting and then transposing the data via the df. T (df.transpose()) method but this throws an error: 'Styler' object has no attribute 'T'.

Any Ideas on how to get this to work?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source