3

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'])

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)  

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.

I tried this:

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'

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 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

3 Comments

what python version are you using, because i still get this error: AttributeError: 'Styler' object has no attribute 'hide_index'
Hi Sam! Sorry for late response. I haven't been on stack for a while. I've been working on a styled dataframe recently (pyhton v3.7.4) and the above syntax still works ok to hide index when exporting to html.
i gave up and now using beautifulSoup, its easier for me :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.