Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas style options to latex

Pandas has two nice functionalities I use a lot - that's the df.style... option and the df.to_latex() call. But I do not know how to combine both.

The .style option makes looking at tables much more pleasant. It lets you grasp information rapidly because of visual enhancements. This works perfectly in a jupyter notebook, for example. Here is an arbitrary example I copied from the documentation.

df.style.bar(subset=['A', 'B'], align='mid', color=['#d65f5f', '#5fba7d'])

This yields:

Dataframe with style columns

However, as nice as this looks in a jupyter notebook, I can not put this to latex code. I get the following error message instead, if chaining a 'to_latex()' call at the end of my visual enhancements: AttributeError: 'Styler' object has no attribute. Does that mean it's simply not possible, because the displayed colorful table is not a DataFrame object any more, but now a Styler object, now?

Is there any workaround? At least with easier tables, let's say where only cells have a single background color with respect to their value, instead of a 'complicated' bar graph.

like image 370
muuh Avatar asked Aug 22 '19 10:08

muuh


People also ask

How do I use conditional formatting in pandas?

One way to conditionally format your Pandas DataFrame is to highlight cells which meet certain conditions. To do so, we can write a simple function and pass that function into the Styler object using . apply() or .

How do I change the color of a Dataframe in Python?

df. style. set_properties: By using this, we can use inbuilt functionality to manipulate data frame styling from font color to background color.


2 Answers

Instead of trying to export this formatting to bulky LaTeX markup, I would go the route explored already over in TeX.SE: add the functionality as LaTeX code that draws similar formatting based on the same data.

like image 189
ojdo Avatar answered Nov 06 '22 13:11

ojdo


As of pandas v1.3.0 these are now combined in pandas.io.formats.style.Styler.to_latex.

like image 2
iacob Avatar answered Nov 06 '22 14:11

iacob