Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove border from html table created via pandas

I am using a python script to display dataframe on a webpage. I used df.to_html to convert my dataframe to HTML. However, by default, it sets the border to 0. I tried overriding it by having a custom css template but it didn't work.

Here is my pandas code:

ricSubscription.to_html(classes='mf')

Is there a parameter I can pass to set the border to zero while making this call?

like image 494
Himanshu Gupta Avatar asked May 29 '15 13:05

Himanshu Gupta


People also ask

How do I stop table borders in HTML?

Table with no outside border. Note: you can use “border: none;” or “border: 0px;”. Either way it results in the outside border being removed from your table.

How do I remove cell borders from a table in CSS?

This is a Default behavior of the table cells that there is some space between their Borders. To remove this space we can use the CSS border-collapsing Property. This Property is used to set the borders of the cell present inside the table and tells whether these cells will share a common border or not.

How do I remove the border color from a table?

Go to Table Tools >Design > Table Styles > Borders, and then click the border option that you want to change. , and then click the borders that you want to delete.


3 Answers

to_html() generates <table border="1" class="dataframe">...

You could just do:

ricSubscription.to_html().replace('border="1"','border="0"')

Also, to answer specifically, there does not appear to be anything you can pass. border="1" appears to be hardcoded:

https://github.com/pydata/pandas/blob/e4cb0f8a6cbb5f0c89b24783baa44326e4b2cccb/pandas/core/format.py#L893

like image 116
Eric Appelt Avatar answered Oct 19 '22 22:10

Eric Appelt


As of version 0.19.0, pandas to_html() borders can be changed in two ways:

  1. Globally: pd.options.html.border = 0
  2. Locally: to_html(border = 0)


UPDATE 2019-07-11:

Per @Hagbard's comment, my original global solution has been deprecated in favor of the following: pd.options.display.html.border = 0


Docs: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_html.html

like image 40
elPastor Avatar answered Oct 19 '22 23:10

elPastor


If you are would like use pd.Styler. You could do something like this :

ricSubscription.style.set_table_attributes(
    'style="border-collapse:collapse"'
).set_table_styles([
    # Rest of styles
]).render()
like image 4
Ajit Avatar answered Oct 20 '22 00:10

Ajit