结合大 pandas "data frame.style";对象并输出到html
问题说明
我想结合使用以下方法的熊猫df.style
对象-df.style.background_gradient
和df.style.bar
,并将结果导出到html.
I would like to combine pandas df.style
objects that use the following methods - df.style.background_gradient
and df.style.bar
and export the result to html.
当我有单独的对象时,我会成功.这是一个示例数据帧:
I'm successful when I have separate objects. Here is an example data frame:
df = pd.DataFrame(np.random.randn(15).reshape(5, 3))
然后我可以将其传递给不同的样式方法:
I can then pass this to the different style methods:
# Use method "background_gradient"
styled_df_a = df.style.background_gradient(subset=[0, 1],
low=0, high=0.5,
cmap="YlGnBu")
# Use method "bar"
styled_df_b = df.style.bar(subset=[2], align='mid',
color=['#d65f5f', '#5fba7d'])
我随后将每个样式表导出到html:
I subsequently export each of the styled tables to html:
# Export styled table a to html
html = styled_df_a.render()
with open("df_a.html","w") as fp:
fp.write(html)
# Export styled table b to html
html = styled_df_b.render()
with open("df_b.html","w") as fp:
fp.write(html)
因此,我有2个html呈现样式表.但是,我想将2种方法组合到1个html样式表中,以使第1-2列具有background_gradient
样式,而第3列具有bar
样式.
I therefore have 2 html rendered styled tables. However, I would like to combine the 2 methods into 1 html styled table, such that columns 1-2 have the background_gradient
styling and column 3 has the bar
styling.
我尝试过:
styled_df_c = styled_df_a.style.bar(subset=[2], align='mid',
color=['#d65f5f', '#5fba7d'])
由于以下错误,此方法不起作用:
This doesn't work due to the following error:
AttributeError: 'Styler' object has no attribute 'style'
是否有其他方法可以做到这一点?我确实尝试过尝试熊猫的style.apply
方法,但是遇到了与上述类似的错误.
Is there a way to do this through some other means? I did try experimenting with the style.apply
method of pandas but got a similar error as above.
正确答案
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfiagj
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20