Highlight the maximum value in each column in Pandas

Highlight the maximum value in each column in Pandas

To highlight the maximum value in each column of a pandas DataFrame, you can use the DataFrame.style property along with the highlight_max() method. The style property returns a Styler object, which has various methods to apply styles based on certain conditions.

Here's how you can highlight the maximum value in each column:

import pandas as pd import numpy as np # Sample DataFrame np.random.seed(0) df = pd.DataFrame(np.random.randn(5, 3), columns=['A', 'B', 'C']) # Highlight the maximum value in each column styled_df = df.style.highlight_max(color='yellow') # To display the styled DataFrame in Jupyter Notebook styled_df 

When you run this in a Jupyter Notebook, you'll see the maximum values in each column highlighted in yellow.

If you want to highlight the maximum values with a different color, just change the color parameter in the highlight_max() method. If you're not running this in a Jupyter environment, and want to generate an HTML representation of this styled DataFrame, you can use the render() method:

html = styled_df.render() print(html) 

This will give you the HTML representation of the styled DataFrame, which you can save or render as needed.


More Tags

excel-2003 rails-activestorage avassetexportsession python-unittest django-permissions launch4j react-router-redux strftime csvhelper client

More Programming Guides

Other Guides

More Programming Examples