18

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

3
  • 9
    Instead of setting a column as index, I'd leave it as a column and use .to_html(index=False) Commented Nov 11, 2016 at 10:47
  • Possible duplicate of pandas: rename axis in df Commented Nov 11, 2016 at 10:47
  • Yeah that method worked perfectly. Thanks @ayhan Commented Nov 11, 2016 at 10:52

2 Answers 2

48

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Also: index_names=False
@EmreSülün what is that?
4

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()

3 Comments

kindly consider adding more information in our answer
Referencing other sources is fine, but you should add an excerpt from said source to your answer showing and explaining the information. Your answer on SO should be complete in itself and offer the link for further reading.
Ok, I provided some additional code to make the answer self-containing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.