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?
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.
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.
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 .
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.
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.
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