Home > Enterprise >  Pandas - How to Save A Styled Dataframe to Image
Pandas - How to Save A Styled Dataframe to Image

Time:01-04

I have styled a dataframe output and have gotten it to display how I want it in a Jupyter Notebook but I am having issues find a good way to save this as an image. I have tried enter image description here

Working Code:

import pandas as pd
import numpy as np
import dataframe_image as dfi

col_name = 'TestColumn'

temp_df = pd.DataFrame({'TestColumn':['A','B','A',np.nan]})

t1 = (temp_df[col_name].fillna("Unknown").value_counts()/len(temp_df)*100).to_frame().reset_index()
t1.rename(columns={'index':' '}, inplace=True)
t1[' '] = t1[' '].astype(str) 


style_test = t1.style.bar(subset=[col_name], color='#5e81f2', vmax=100, vmin=0).set_table_attributes('style="font-size: 17px"').set_properties(
    **{'color': 'black !important',
       'border': '1px black solid !important'}
).set_table_styles([{
    'selector': 'th',
    'props': [('border', '1px black solid !important')]
}]).set_properties( **{'width': '500px'}).hide_index().set_properties(subset=[" "], **{'text-align': 'left'})

dfi.export(style_test, 'successful_test.png')
  •  Tags:  
  • Related