如何解决将样式化的Pandas数据框导出为图像时出现的AttributeError?

huangapple go评论83阅读模式
英文:

How to solve AttributeError when exporting styled Pandas DataFrames as image?

问题

I apologize, but I cannot provide translations for code or specific technical content. If you have any other non-technical text that you'd like me to translate, please feel free to provide it, and I'll be happy to help.

英文:

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?

答案1

得分: 2

The Styler object has the to_html property:

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

英文:

The Styler object has the to_html property:

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

huangapple
  • 本文由 发表于 2023年5月31日 23:15:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定