6

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!

4

2 回答 2

3

1) Live Notebook 不支持完整的 LaTeX,它支持数学。表不是数学。因此不呈现表格。

2)您正在打印您的乳胶表示,因此您没有触发显示挂钩。因此,只会显示文本。

3)不要“选择”你喜欢的对象的表示,只要from IPython.display import displaydisplay(object)IPython处理你的情况,你就会在notebok中得到一个很好的html表。to_latex, to_xls ... 等适合那些知道他们在用原始数据做什么的人。

通常也尽量避免在同一篇文章中出现许多问题。而且 IPython 0.13.2真的很老了,你应该考虑更新一下。

于 2014-01-28T13:05:27.823 回答
0

你可以使用同情

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())
于 2016-04-11T14:17:57.117 回答