1

I would like to export a jupyter notebook cell as a png image.

import pandas as pd
import numpy as np

np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
               axis=1)
df.iloc[3, 3] = np.nan
df.iloc[0, 2] = np.nan
df.style
df_styled = df.style.highlight_max(axis=0)
df_styled

If I run this on a jupyter notebook, the output is a nice representation of the table. I wonder if it's possible to get this representation as an image.

L. Rebelo
  • 87
  • 3
  • 9
  • Does this answer your question? [How to Display Dataframe next to Plot in Jupyter Notebook](https://stackoverflow.com/questions/45286696/how-to-display-dataframe-next-to-plot-in-jupyter-notebook) – Brown Bear Feb 07 '20 at 13:44
  • @BearBrown, that is not what they are asking. They want to save the dataframe table as an image. – James Feb 07 '20 at 13:48
  • Does this answer your question? [How to save a pandas DataFrame table as a png](https://stackoverflow.com/questions/35634238/how-to-save-a-pandas-dataframe-table-as-a-png) – roman_ka Feb 07 '20 at 13:48
  • @roman_ka That works, but doesn't really answer my question. I want to be able to have the same output I get when I display a pandas DataFrame but have it as an image file. – L. Rebelo Feb 07 '20 at 14:34

2 Answers2

2

I've found the best solution to be dataframe2img. It's not perfect, but it's the closest thing I found.

L. Rebelo
  • 87
  • 3
  • 9
  • 1
    Pleasingly stumbling upon your use of my script, I looked into this realm again and found [this post](https://stackoverflow.com/a/63387275/8508004) where it mentions [dataframe_image](https://github.com/dexplo/dataframe_image). (Looks like some people wanted this ability, too, and started their own effort after mine.) It looks better developed than mine. I'm planning to put a note about it on the page you link to at my repo. – Wayne Dec 06 '22 at 17:19
1

df_styled is not a dataframe, but a Styler object, so cannot be converted to a plot, what you need to do is to render it to html and then use a separate library to export it. Please see this issue for explanation:

Export pandas Styled table to image file

roman_ka
  • 479
  • 3
  • 12
  • That helped but the table in the image I got didn't have the same styling as when jupyter displays a pandas dataframe (white and grey rows for example). Do you know if there is a way to get there? – L. Rebelo Feb 07 '20 at 16:11
  • I am afraid I don't. Can't think of anything apart from taking a screenshot – roman_ka Feb 07 '20 at 16:36