2

I'm trying to style some rows of a Pandas Dataframe a certain way (depending on an external factor) using df.to_html() and some CSS rules, but is there a way to give rows HTML IDs using Pandas, or will I have to deal with the raw HTML output of to_html().

More details: I have a Pandas Dataframe that is the result of an SQL query. It will be compared with previous query results and the table will be sent out as an email. I want to highlight the rows of the dataframe that are new. I'm thinking of doing this by inserting CSS rules that either apply to the IDs of specific rows in the resultant HTML table, or by attaching a class to these rows.

1 Answer 1

1

You can use the .to_html() classes keyword (see docs) to attach classes to the <table></table> tag. If you need id instead of class, you could fix the output with df.to_html(classes='my_class').replace('class', 'id').

With brand new version 0.17.1, pandas received conditional html formatting that allows more fine-grained layout control - under development: see docs..

In particular, the slicing functionality sounds like what you are looking for to highlight specific rows. ipython notebook with documentation and interactive examples: here

For more complicated adjustments beyond the <table> level there is always BeautifulSoup, but that's of course ex-post and not part of pandas.

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

2 Comments

Whoa, slicing is exactly what I'm looking for! I'm probably not going to use it for this specific job (because it's under development and I need something stable for work), but if I need this going forward, I'm hitting it up. Thanks!
I just have to say, having to do a find/replace on to_html is the primary work-around I have seen... but there really shouldn't be a work-around since ids in HTML are pretty common. And since this is an HTML-related function, you'd think it would have this OBVIOUSLY built in... but no.

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.