Pandas Dataframe 到 HTML 删除索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40546119/
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-14 02:25:05  来源:igfitidea点击:

Pandas Dataframe to HTML remove index

pythonhtmlpandasdataframeflask

提问by Bchadwick

I'm using a panda dataframe to read csv data into a Flask project. I'm trying to remove the Dataframe indexes in the HTML table using the set_index method:

我正在使用Pandas数据框将 csv 数据读入 Flask 项目。我正在尝试使用 set_index 方法删除 HTML 表中的 Dataframe 索引:

overviewTable.set_index('Tower Number', inplace=True)

When I use this Method the Tower Number Header jumps down a row below all the other headers.

当我使用此方法时,塔号标题会在所有其他标题下方跳下一行。

The HTML looks like this:

HTML 如下所示:

<div class="row table-responsive">
   <div class="tower-table">
       {{ overview|safe }}
   </div>
</div>

and the Python:

和 Python:

overview = pandas.read_csv('../overview_table.csv')
overviewTable = overview[cols]
overviewTable.set_index('Tower Number', inplace=True)

@app.route('/')
def dash():

    return render_template('dash.html', overview=overviewTable[1:167].to_html())

And the CSS:

和 CSS:

.tower-table {
overflow-x: hidden;
overflow-y: scroll;
width: 100%;
height: 500px;
background-color: darkgrey;
border-color: #003430;
border-radius: 5px;
}
.tower-table tr {
height: 50px;
}
.tower-table thead tr  {
height: 100px;
border-top: none;
}

Is there another method to remove the indexes without affecting the headers. Or is there anything I could do in the CSS etc to stop the header moving down a row

是否有另一种方法可以在不影响标题的情况下删除索引。或者我可以在 CSS 等中做些什么来阻止标题向下移动一行

回答by elevendollar

As mentioned by @ayhan in the comments you can use .to_html(index=False)to surpress the index to be included in the html table.

正如@ayhan 在评论中提到的,您可以.to_html(index=False)用来抑制要包含在 html 表中的索引。

Just posted it because not everyone might see the comment.

刚刚发布它,因为不是每个人都可能看到评论。

回答by Hagbard

As mentioned here, one can now use the "hide_index()" function in Pandas (see https://pandas.pydata.org/pandas-docs/stable/style.html#Hiding-the-Index-or-Columns) just before calling "render()":

正如这里提到的,现在可以在 Pandas 中使用“hide_index()”函数(参见https://pandas.pydata.org/pandas-docs/stable/style.html#Hiding-the-Index-or-Columns)在调用“render()”之前:

def highlight_oddRow(s):
    return ['background-color: yellow' if s.name % 2 else '' for v in s]

table = pd.DataFrame(
    {'a': [1,2,3,4,5], 'b': [6,7,8,9,10]})

with open ('out.html','w') as out:
    print >> out, table.style.apply(highlight_oddRow,axis=1).hide_index().render()