Question

I am unable to visualize the Latex table generated using dataframe.to_latex() from pandas in my IPython notebook. It shows the exact string with the "\begin.." line in a box.

I am also curious why the table is formatted {lrrrrr} and how I can change it columns with lines separating the values like {l|c|c|c|c}.

I'm not quite sure if my setup is the issue and I am wondering if there are further documentation for formatting Latex rendered tables using pandas.dataframe.to_latex(). I use IPython notebook (0.132) and Pandas (0.13). I'm running Ubuntu 13.04, Texlive2012.

IPython Notebook code:

df = pd.DataFrame(np.random.random((5, 5)))
print df.to_latex()

IPython notebook output even after copying and running as markdown, only a box around the text is added.

\begin{tabular}{lrrrrr}
\toprule
{} &         0 &         1 &         2 &         3 &         4 \\
\midrule
0 &  0.021896 &  0.716925 &  0.599158 &  0.260573 &  0.665406 \\
1 &  0.467573 &  0.235992 &  0.557386 &  0.640438 &  0.528914 \\
2 &  0.872155 &  0.053389 &  0.419169 &  0.613036 &  0.606046 \\
3 &  0.130878 &  0.732334 &  0.168879 &  0.039845 &  0.289991 \\
4 &  0.247346 &  0.370549 &  0.906652 &  0.228841 &  0.766951 \\
\bottomrule
\end{tabular}

I would appreciate any help as I am still very new to pandas and the wonderful things it can do with the SciPy suite!

Was it helpful?

Solution

1) Live Notebook does not support full LaTeX it support Math. Table are not math. Hence table are not rendered.

2) You are printing your latex representation, so you are not triggering the display hook. Hence only the text will show.

3) do not "choose" the representation of on object you like, just from IPython.display import display and display(object) let IPython handle the rest in your case you will get a nice html table in notebok . to_latex, to_xls ... etc are ment for people that know what they are doing with the raw data.

Also in general try to avoid many question in the same post. and IPython 0.13.2 is really old, you should think of updating.

OTHER TIPS

You can use sympy

import pandas as pd
import numpy as np
from sympy import *
init_printing()
df = pd.DataFrame(np.random.random((5, 5)))
Matrix(df.as_matrix())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top