Martin Witkowski
Martin Witkowski

Reputation: 121

pandas Styler. How to ignore the index column from the rendered HTML

I am trying to use the string generated from rendering a styler in an email message. Seems really hard to get this to ignore the dataframe index.

table_styles = [dict(selector="tbody tr th", props=[("display", "none")]),
st=df.style.set_table_styles(table_styles)
st.render()

I have been able to sort of make it work with the display none CSS setting, but it works differently on different devices based on CSS support level.
Isn't there a way to make the index payload just go away ?

Upvotes: 9

Views: 7821

Answers (5)

D.Thomas
D.Thomas

Reputation: 359

styler.hide_index() method is deprecated in favour of `Styler.hide(axis='index') (version 1.4.0)

for me df.style.hide(axis='index') works.

Upvotes: 4

Raskolnikov
Raskolnikov

Reputation: 1

Since version 0.23.0 pandas comes with a styler.hide_index() function, chain it with other methods to be applied as the returned object is still going to be a styler object

If hide_index() is not available, try updating your pandas library

Upvotes: 0

An economist
An economist

Reputation: 1311

According to pandas styling documentation section: Hiding the Index or Columns it is enough to use .hide_index(). In your case it would be:

table_styles = [dict(selector="tbody tr th", props=[("display", "none")]),
st=df.style.set_table_styles(table_styles).hide_index()
st.render()

Upvotes: 1

Michele Pardini
Michele Pardini

Reputation: 13

I am not sure we had the same issue but my problem was to save a df into an HTML file using .style.render(). I've tried to use the CSS style but it wouldn't work. The solution I used is so simple I didn't think about it, I basically run .hide_index() , like this:

html = ts_volume_html.style.format({'total_ops': "{:.2f}", 'read_ops': '{:.2f}','read_data(bps)':'{:.2f}',
                        'read_latency(us)': "{:.2f}", 'write_ops': '{:.2f}','write_data(bps)':'{:.2f}',
                        'write_latency(us)': "{:.2f}", 'other_ops': '{:.2f}','other_latency(us)':'{:.2f}', \
                        .set_table_attributes('border="1" class="dataframe table table-hover table-bordered"')\
                        .set_precision(2).set_properties(**{'font-size': '10pt', 'font-family': 'Courier New'})\
                        .hide_index().background_gradient(cmap=cm).set_table_styles([{'selector': 'th', 'props': [('font-size', '10pt')]}]).render()

I then saved the 'html' str into an html file.

Upvotes: 0

pg2455
pg2455

Reputation: 5168

I think I have the same solution as yours and I am facing the same issue as you were (display is different on different devices). I am just writing down the partial solution here to help someone who is in search of the way to do it.

if you do html.render().split('\n') you will be able to get the class structure related to the first column and index( if you have already used resent_index).

Styles can then be defined to get rid of those columns using CSS property of display.

# define the style related to first column and index here
# first-element : when index =True, 
# second element: default index of the table generated by the Style
styles = [ 
  dict(selector = ".col0", props = [('display', 'none')]), 
  dict(selector = "th:first-child", props = [('display', 'none')])
 ]

 # set the table styles here
 table.set_table_styles(styles)

Upvotes: 1

Related Questions