Is it possible to get a nicely formatted table from a pandas dataframe in ipython notebook when using nbconvert to latex & PDF?
The default seems to be just a left-aligned block of numbers in a shoddy looking font.
I would like to have something more like the html display of dataframes in the notebook, or a latex table. Saving and displaying a .png image of an HTML rendered dataframe would also be fine, but how exactly to do that has proved elusive.
Minimally, I would just like a simple centre-aligned table in a nice font.
I haven't had any luck with various attempts to use the .to_latex() method to get latex tables from pandas dataframes, either within the notebook or in nbconvert outputs. I've also tried (after reading ipython dev list discussions, and following the custom display logic notebook example) making a custom class with _repr_html_ and _repr_latex_ methods, returning the results of _to_html() and _to_latex(), respectively. I think a main problem with the nb conversion is that pdflatex isn't happy with either the {'s or the //'s in the dataframe to_latex() output. But I don't want to start fiddling around with that before checking I haven't missed something.
Thanks.
There is a simpler approach that is discussed in this Github issue. Basically, you have to add a
_repr_latex_
method to the DataFrame class, a procedure that is documented from pandas in their official documentation.I did this in a notebook like this:
The following code:
turns into an HTML table if evaluated live in the notebook, and it converts into a (centered) table in PDF format:
I wrote my own
mako
-based template scheme for this. I think it's actually quite an easy workflow if you commit to chugging through it for yourself once. After that, you begin to see that templating the metadata of your desired format so it can be factored out of the code (and doesn't represent a third-party dependence) is a very nice way to solve it.Here is the workflow I came up with.
Write the .mako template that accepts your dataframe as an argument (and possibly other args) and converts it to the TeX format you want (example below).
Make a wrapper class (I call it
to_tex
) that makes the API you desire (e.g. so you can pass it your data objects and it handles the call tomako
render commands internally).Within the wraper class, decide on how you want the output. Print the TeX code to the screen? Use a subprocess to actually compile it to a pdf?
In my case, I was working on generating preliminary results for a research paper and needed to format tables into a complicated double-sorted structure with nested column names, etc. Here's an example of what one of the tables looks like:
Here is the mako template for this (warning, gross):
My wrapper
to_tex.py
looks like this (with example usage in theif __name__ == "__main__"
section):My choice was to directly pump the TeX string to
pdflatex
and leave as an option to display it.A small snippet of code actually using this with a DataFrame is here: