Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get rid of extra slashes in pandas to_latex function?

I am trying to convert pandas data frame to latex format but it’s returning extra slashes in the output.

Data

df = pd.DataFrame({'model_name': {0: 'ALBERT', 1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}, 'macro_precision_first': {0: 91.89396817624747, 1: 92.17133890858452, 2: 92.6826295632407, 3: 92.80299948526579, 4: 93.34410168004217, 5: 92.0346638078341}, 'macro_recall_first': {0: 92.2318448502444, 1: 92.3357692328841, 2: 93.0292033013862, 3: 93.12015088280738, 4: 93.66748596470968, 5: 92.36958589707638}, 'macro_f1_first': {0: 91.95372391665576, 1: 92.23312713937592, 2: 92.71221623765801, 3: 92.88621653320901, 4: 93.43123707996612, 5: 92.1035266749227}})

When I am trying df.to_latex(), the output looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

'\\begin{tabular}{llrrr}\n\\toprule\n{} & model\\_name &  macro\\_precision\\_first &  macro\\_recall\\_first &  macro\\_f1\\_first \\\\\n\\midrule\n0 &     ALBERT &                  91.89 &               92.23 &           91.95 \\\\\n1 &          a &                  92.17 &               92.34 &           92.23 \\\\\n2 &          b &                  92.68 &               93.03 &           92.71 \\\\\n3 &          c &                  92.80 &               93.12 &           92.89 \\\\\n4 &          d &                  93.34 &               93.67 &           93.43 \\\\\n5 &          e &                  92.03 &               92.37 &           92.10 \\\\\n\\bottomrule\n\\end{tabular}\n'

What I am expecting is the rows like this:

ALBERT &                  91.89 &               92.23 &           91.95 \\ 
a      &                  92.17 &               92.34 &           92.23 \\
b      &                  92.68 &               93.03 &           92.71 \\
c      &                  92.80 &               93.12 &           92.89 \\
d      &                  93.34 &               93.67 &           93.43 \\
e      &                  92.03 &               92.37 &           92.10 \\

How I can get rid of extra slashes and \\\n?

>Solution :

The slashes are escape characters in the string. They are not really present. Try to print:

print(df.to_latex())

output:

\begin{tabular}{llrrr}
\toprule
{} & model\_name &  macro\_precision\_first &  macro\_recall\_first &  macro\_f1\_first \\
\midrule
0 &     ALBERT &              91.893968 &           92.231845 &       91.953724 \\
1 &          a &              92.171339 &           92.335769 &       92.233127 \\
2 &          b &              92.682630 &           93.029203 &       92.712216 \\
3 &          c &              92.802999 &           93.120151 &       92.886217 \\
4 &          d &              93.344102 &           93.667486 &       93.431237 \\
5 &          e &              92.034664 &           92.369586 &       92.103527 \\
\bottomrule
\end{tabular}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading