Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the `to_latex` method of pandas in Jupyter to obtain code that one can use directly in LaTeX?

Consider the multi-level index dataframe s

import numpy as np
import pandas as pd
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
   ...:           ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
s = pd.DataFrame(np.random.randn(8, 4), index=arrays)
s

How may I convert it to a nice table in latex using python codes?

I tried

print s.to_latex()

but it failed and gave me this result

File "<ipython-input-45-d2f4611ecc13>", line 1
    print s.to_latex()
          ^
SyntaxError: invalid syntax

I also tried

s.to_latex()

And the result was not organized at all

'\\begin{tabular}{llrrrr}\n\\toprule\n    &     &         0 &         1 &         2 &         3 \\\\\n\\midrule\nbar & one & -0.008518 & -0.535653 & -0.603135 &  0.891277 \\\\\n    & two &  0.191005 & -1.470384 &  0.697245 &  1.054424 \\\\\nbaz & one &  0.414505 & -0.312967 &  0.703490 &  0.933851 \\\\\n    & two & -0.295505 & -0.923843 & -0.423998 &  0.160162 \\\\\nfoo & one & -1.339702 &  0.616059 &  0.519983 &  0.554584 \\\\\n    & two & -1.003784 &  0.674981 &  1.609906 &  0.274486 \\\\\nqux & one & -0.139274 & -0.783004 &  1.084794 &  2.202673 \\\\\n    & two & -0.730785 & -0.468041 &  0.762726 & -0.532599 \\\\\n\\bottomrule\n\\end{tabular}\n'
like image 724
rsc05 Avatar asked Mar 10 '23 21:03

rsc05


1 Answers

I was breaking my head with this the whole day. I'd assume that you, like me, were working in a Jupyter environment and wanted to copy your results to a LaTex source.

In order to do that, you can surround the s.to_latex with a print() instruction and the output will be what you need for your LaTeX document.

In your example.

import numpy as np
import pandas as pd
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
s = pd.DataFrame(np.random.randn(8, 4), index=arrays)
print(s.to_latex())
\begin{tabular}{llrrrr}
\toprule
    &     &         0 &         1 &         2 &         3 \\
\midrule
bar & one &  0.371649 & -0.898747 &  1.996738 &  0.051273 \\
    & two & -0.409381 & -0.415512 & -1.975661 & -0.233850 \\
baz & one &  1.014193 & -0.789155 &  0.046733 &  0.142827 \\
    & two & -1.831074 & -1.743929 & -0.778236 &  1.408626 \\
foo & one &  0.040501 & -0.232053 &  0.979267 &  0.709616 \\
    & two & -0.407516 & -0.036641 & -0.286853 & -1.320493 \\
qux & one & -1.770540 &  0.490122 &  0.745257 & -0.921023 \\
    & two & -0.025855 & -1.567346 & -1.646400 & -2.411050 \\
\bottomrule
\end{tabular}
like image 124
EloyRD Avatar answered Apr 09 '23 04:04

EloyRD