0

Why do I keep getting the AttributeError: 'Styler' object has no attribute 'render' when trying to save my Pandas DataFrame as an image? I made a correlation matrix of the variables in a dataframe through df.corr() and stylized it.

import pandas as pd
import dataframe_image as dfi
import imgkit

df = pd.read_excel(filename)
CM = df.corr() # CM stands for correlation matrix
styled_CM = CM.style.background_gradient(cmap="Blues")

# either of the following options:
html = styled_CM.render()
imgkit.from_string(html, 'styled_CM.png')

# or this one:
dfi.export(styled_CM, 'styled_CM.png')

Both options return the following

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

How to work solve this or what alternative to use?

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
Pimsel
  • 3
  • 2
  • [This question](https://stackoverflow.com/questions/67869034/styler-object-has-no-attribute-style) addresses a similar issue. `styled_CM` is not a CM object, but instead a styler object. – SimonUnderwood May 31 '23 at 16:03

1 Answers1

2

The Styler object has the to_html property:

html = styled_CM.to_html() instead of html = styled_CM.render()

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
Laura
  • 51
  • 3