Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Dataframe to HTML remove index

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

like image 833
Bchadwick Avatar asked Nov 11 '16 10:11

Bchadwick


2 Answers

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.

like image 167
elevendollar Avatar answered Oct 10 '22 09:10

elevendollar


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()
like image 34
Hagbard Avatar answered Oct 10 '22 08:10

Hagbard