结合大 pandas "data frame.style";对象并输出到html [英] Combining pandas "data frame.style" objects and outputting to html

查看:238
本文介绍了结合大 pandas "data frame.style";对象并输出到html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

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

I've found a solution based on info here: mode.com/example-gallery/python_dataframe_styling

您可以尝试将多个样式方法同时传递给同一数据框,而不是尝试使用更多样式方法来强制样式对象.请参见以下内容:

Rather than trying to coerce a styled object with more styling methods, you can simply pass multiple style methods to the same data frame simultaneously. Please see below:

# example data frame
df = pd.DataFrame(np.random.randn(15).reshape(5, 3))

# pass multiple style methods to same  data frame
styled_df = (df.style
             .background_gradient(subset=[0, 1], # background_gradient method
                                  low=0, high=0.5,
                                  cmap="YlGnBu")
             .bar(subset=[2], align='mid',       # bar method
                  color=['#d65f5f', '#5fba7d']))

# Export "styled_df" to html
html = styled_df.hide_index().render()
with open("html_c.html","w") as fp:
   fp.write(html)

这篇关于结合大 pandas "data frame.style";对象并输出到html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆