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:
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:
<div class="row table-responsive">
<div class="tower-table">
{{ overview|safe }}
</div>
</div>
and the 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:
.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
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.
Just posted it because not everyone might see the comment.
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()":
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With