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.
The simplest way available now is to display your dataframe as a markdown table. You may need to install tabulate
for this.
In your code cell, when displaying dataframe, use following:
from IPython.display import Markdown, display
display(Markdown(df.to_markdown()))
Since it is a markdown table, nbconvert can easily translate this into latex.
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:
import pandas as pd
pd.set_option('display.notebook_repr_html', True)
def _repr_latex_(self):
return "\centering{%s}" % self.to_latex()
pd.DataFrame._repr_latex_ = _repr_latex_ # monkey patch pandas DataFrame
The following code:
d = {'one' : [1., 2., 3., 4.],
'two' : [4., 3., 2., 1.]}
df = pd.DataFrame(d)
df
turns into an HTML table if evaluated live in the notebook, and it converts into a (centered) table in PDF format:
$ ipython nbconvert --to latex --post PDF notebook.ipynb
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With