• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

结合大 pandas "data frame.style";对象并输出到html

用户头像
it1352
帮助1

问题说明

我想结合使用以下方法的熊猫df.style对象-df.style.background_gradientdf.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.

正确答案

#1

我在此处找到了基于信息的解决方案:

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfiagj
系列文章
更多 icon
同类精品
更多 icon
继续加载