css pandas 的文件 - 相关性 table

css file for pandas - correlation table

当我用jupyter写代码时:

import pandas as pd

df = pd.read_csv("path..")
corr = df.corr()
corr.style.background_gradient(cmap='coolwarm')

我会得到 table 颜色和样式。但是,当我添加样式并渲染它时。

style_html = corr.style.render()
print(corr.style.render())

我可以看到输出:

... style type="text/css" /style 等

注意:我从上面的 html 代码中删除了<> ...

检查 HTML (png):

我注意到系统正在获取 text/css 文件。但我不知道这个 css 文件在哪里可用,我猜测是因为 CSS 文件在我的本地驱动器中不可用。因此,当我在渲染后将语法传递给 QWebEngineView 时,它会显示纯输出。所以有人请帮助我如何在 QwebEngineView

中获得与颜色和样式的相关性 table

当您设置 background_gradient 时,您不是在修改默认的 Styler,而是创建一个新的,因此您必须获得新 Styler 的 html:

import sys

import pandas as pd

from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView

app = QApplication(sys.argv)

web = QWebEngineView()

df = pd.DataFrame(
    [(0.2, 0.3), (0.0, 0.6), (0.6, 0.0), (0.2, 0.1)], columns=["dogs", "cats"]
)
corr = df.corr()
style_html = corr.style.background_gradient(cmap="coolwarm").render()

web.setHtml(style_html)
web.resize(320, 120)
web.show()

sys.exit(app.exec_())