Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DataFrame styler HTML display without index?

Tags:

html

pandas

I have a pandas dataframe, I'm using the df.style object to make it highlight odd-numbered rows, so:

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

table = pd.DataFrame(
    {'a': [3,9,8,0,2], 'b': [5,95, 9, 25,5], 'c': [23,54, 2, 3,5], 'row': [1, 2, 3, 4, 5]})

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

However, this always prints out the index. Is there a way to tell it not to do this?

like image 670
Brian Postow Avatar asked Mar 15 '17 14:03

Brian Postow


People also ask

Can I have a Dataframe without index?

To Print DataFrame Without Index By Making Index emptyYou can set the index as empty for each row, you can do this by creating an array with the empty string (one for each row in the DataFrame). and assign this to the DataFrame.

How do I hide the index of a data frame?

Data Frame has a property named "index" which is set to true by default. If you want to print values without index then you have to set this property to false.

How do I ignore index in Python?

Dealing with index and axis If you want the concatenation to ignore existing indices, you can set the argument ignore_index=True . Then, the resulting DataFrame index will be labeled with 0 , …, n-1 . To concatenate DataFrames horizontally along the axis 1 , you can set the argument axis=1 .

How do I print a data frame without the header?

Print the DataFrame with headers. Use read_csv method to get the DataFrame with tab separator and without headers. To read without headers, use header=0. Print the DataFrame without headers.


1 Answers

Since this is the first question that popped up when I searched for this issue on Stack Overflow, I thought it would be good to share a recent development: on Nov-17 a PR was committed to the pandas repo that added the hide_index method to styler objects.

You can just call it before render:

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

table = pd.DataFrame(
    {'a': [3,9,8,0,2], 'b': [5,95, 9, 25,5], 'c': [23,54, 2, 3,5], 'row': [1, 2, 3, 4, 5]})

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

Keep in mind the docs still claim these features are provisional and subject to change. More info here: https://pandas.pydata.org/pandas-docs/stable/style.html#Hiding-the-Index-or-Columns.

like image 194
Tomas Farias Avatar answered Oct 18 '22 23:10

Tomas Farias