从通过 Pandas 创建的 html 表中删除边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30531374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:24:46  来源:igfitidea点击:

Remove border from html table created via pandas

pythonhtmlpandas

提问by Himanshu Gupta

I am using a python script to display dataframe on a webpage. I used df.to_htmlto 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.

我正在使用 python 脚本在网页上显示数据框。我曾经df.to_html将我的数据框转换为 HTML。但是,默认情况下,它将边框设置为 0。我尝试通过自定义 css 模板来覆盖它,但没有奏效。

Here is my pandas code:

这是我的Pandas代码:

ricSubscription.to_html(classes='mf')

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

在进行此调用时,是否可以传递参数以将边框设置为零?

回答by Eric Appelt

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

to_html()产生 <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:

另外,具体回答一下,似乎没有什么可以通过的。border="1"似乎是硬编码的:

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

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

回答by elPastor

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

从 0.19.0 版本开始,to_html()可以通过两种方式更改Pandas边框:

  1. Globally: pd.options.html.border = 0
  2. Locally: to_html(border = 0)
  1. 全球: pd.options.html.border = 0
  2. 本地: to_html(border = 0)



UPDATE 2019-07-11:

更新 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

根据@Hagbard 的评论,我原来的全局解决方案已被弃用,取而代之的是以下内容: pd.options.display.html.border = 0


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


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

回答by Ajit

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

如果您想使用pd.Styler. 你可以这样做:

ricSubscription.style.set_table_attributes(
    'style="border-collapse:collapse"'
).set_table_styles([
    # Rest of styles
]).render()