Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubleshooting Latex table from pandas dataframe to_latex()

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!

like image 511
Albino_coffee Avatar asked Jan 28 '14 10:01

Albino_coffee


People also ask

How do I retrieve a column from a data frame?

You can use the loc and iloc functions to access columns in a Pandas DataFrame. Let's see how. If we wanted to access a certain column in our DataFrame, for example the Grades column, we could simply use the loc function and specify the name of the column in order to retrieve it.

What is Syntaxfor Panda's data frame?

For the row labels, the Index to be used for the resulting frame is Optional Default np. arange(n) if no index is passed. For column labels, the optional default syntax is - np. arange(n).

What does .values in pandas do?

The values property is used to get a Numpy representation of the DataFrame. Only the values in the DataFrame will be returned, the axes labels will be removed. The values of the DataFrame. A DataFrame where all columns are the same type (e.g., int64) results in an array of the same type.


1 Answers

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.

like image 174
Matt Avatar answered Oct 25 '22 00:10

Matt