Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 358
Martin Witkowski Avatar asked Jan 11 '16 04:01

Martin Witkowski


People also ask

Can we remove the index column in pandas?

Drop the index column of Pandas DataFrame We can remove the index column in existing dataframe by using reset_index() function. This function will reset the index and assign the index columns start with 0 to n-1. where n is the number of rows in the dataframe.

How do I hide the index of a column in a data frame?

Use hide_index() By using hide_index() method, you can hide the Index of the DataFrame.

How do I ignore index in Python?

Practical Data Science using Python To create a DataFrame from DateTimeIndex ignoring the index, use the DateTimeIndex. to_frame() method. Set the parameter index to False to ignore the index.


1 Answers

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)
like image 173
pg2455 Avatar answered Sep 21 '22 20:09

pg2455