PythonF
PythonF

Reputation: 496

LaTeX Table with uncertainty from pandas Dataframe

I am currently working on a report with many values and uncertainties calculated in python and stored in a pandas DataFrame.

These Values have to be put inside the report including the errors. Currently my only way to do so, is to manually combine the values with the errors. One example of this looks like the following:

\begin{tabular}{cccc}
    \hline
    Konzentration $c/\si{\gram\per\liter}$ & $D_\text{app} / \si{\square\meter\per\second}$ & $R_h / \si{\meter}$ & PDI  \\
    \hline
    \SI{0.50}{} & \SI{9.9(2)e-13}{}  & \SI{3.84(8)e-7}{} & \SI{4.1(3)}{} \\
    \SI{0.33}{} & \SI{6.35(7)e-13}{} & \SI{5.96(6)e-7}{} & \SI{1.4(2)}{} \\
    \SI{0.25}{} & \SI{1.16(2)e-12}{} & \SI{3.26(6)e-7}{} & \SI{2.6(2)}{} \\
    \SI{0.20}{} & \SI{6.11(9)e-15}{} & \SI{6.20(9)e-7}{} & \SI{4.9(3)}{} \\
    \hline
\end{tabular}

enter image description here

I am aware of pd.to_latex() which partially decreased the amount of work but does as far is I know not work with erros.

Are you aware of any way on either LaTeX or python to achieve such a table without manually combining values and errors. The DataFrame looks like the following: enter image description here

The only idea I had so far, was to write a function which takes in the value, error and the size of the uncertainty (in this case always 1) and returns a string which I can use with pd.to_latex().

Is there an easier or likely better way to do so?

Upvotes: 4

Views: 1429

Answers (1)

PythonF
PythonF

Reputation: 496

I have written a function which combines the values and errors in a single string.

def conv2siunitx(val, err, err_points=1):
    val = f'{val:.20e}'.split('e')
    err = f'{err:.20e}'.split('e') 
    first_uncertain = int(val[1]) - int(err[1]) + err_points

    my_val = f'{np.round(float(val[0]), first_uncertain-1):.10f}'
    my_err = f'{np.round(float(err[0]), err_points-1):.10f}'.replace('.','')
    # Avoid 1. and write 1 instead
    if first_uncertain > 1:
        first_uncertain = first_uncertain + 1

    return(f'{my_val[:first_uncertain]}({my_err[:err_points]})e{val[1]}')

In combination with S-columns this leads to the result I was looking for.

Upvotes: 3

Related Questions