0

Context: I am writing a bot on Databricks using Python that will send to a Slack channel the image of a pandas dataframe table. That table was formatted using .style to make it faster for people to see the most important numbers.

I have two problems:

  1. how can I save as an image a pandas dataframe that went through the .style method?
  2. how can I open that image in another Databricks notebook?

Step 1 - OK: generating a sample dataframe.

import pandas as pd

my_df = pd.DataFrame({'fruits':['apple','banana'], 'count': [1,2]})

Step 2 - OK: then, I save a new variable in the following way to add to the table several formatting modifications that I need:

my_df_styled = (my_df.style
     .set_properties(**{'text-align': 'center', 'padding': '15px'})
     .hide_index()
     .set_caption('My Table')
     .set_table_styles([{'selector': 'caption', 
                         'props': [('text-align', 'bottom'),
                                   ('padding', '10px')
                                  ]}])
    )

Step 3 - Problem: trying to save the new variable as an image. But here, I am not being able to correctly do it. I tried to follow what was mentioned here, but they are using matplotlib to save it and it is something that I don't want to do, because I don't want to lose the formatting on my table.

my_df_styled.savefig('/dbfs/path/figure.png')

But I get the following error:

AttributeError: 'Styler' object has no attribute 'savefig'

Step 4 - Problem: opening the image in a different notebook. Not sure how to do this. I tried the following using another image:

opening_image = open('/dbfs/path/otherimage.png')

opening_image

But instead of getting the image, I get:

Out[#]: <_io.TextIOWrapper name='/dbfs/path/otherimage.png' mode='r' encoding='UTF-8'>

Rafael Pinheiro
  • 393
  • 4
  • 16

1 Answers1

0

For first question, savefig() is the method of Matplotlib so it is certainly not working if you try to do sth like df.savefig() Rather, you should use another wrapper (matplotlib or other library in below link) to input the dataframe so that the dataframe can be converted into image with desired style. https://stackoverflow.com/a/69250659/4407905

For the second question, I do not try Databrick myself, but I guessed it would be better if you do use to_csv(), to_excel(), to_json(), etc. method to export data into text-based format.

Frederick Li
  • 558
  • 6
  • 5