Highlight the maximum value in each column in Pandas

In data analysis tasks, identifying the maximum values within each column of a Pandas DataFrame is crucial for gaining insights and understanding the data. Python's Pandas library provides various techniques to highlight these maximum values, making them visually distinguishable for quick analysis and decision-making.

This article explores different methods to highlight maximum values in each column using Pandas styling capabilities.

Using Styler.highlight_max()

The highlight_max() method is the simplest way to highlight maximum values in each column. It automatically applies styling to the highest value in each column.

Example

import pandas as pd
import numpy as np

# Create sample data
data = {'Name': ['Sai', 'Prema', 'Akrit', 'Suchitra', 'Abhimanu'],
        'Age': [20, 23, 41, 29, 32],
        'Marks': [92, 84, 35, 88, 83]}

df = pd.DataFrame(data)

# Display original DataFrame
print("Original DataFrame:")
print(df)
Original DataFrame:
       Name  Age  Marks
0       Sai   20     92
1     Prema   23     84
2     Akrit   41     35
3  Suchitra   29     88
4  Abhimanu   32     83
# Highlight maximum values with custom color
styled_df = df.style.highlight_max(color='lightblue', axis=0)
print("Maximum values highlighted (Age=41, Marks=92)")
Maximum values highlighted (Age=41, Marks=92)

The highlight_max() method accepts parameters like color for customization and axis=0 to apply highlighting column-wise.

Using apply() with Custom Function

For more control over the styling, you can create a custom function and apply it using the apply() method.

Example

import pandas as pd

# Create sample data
data = {'Name': ['Sai', 'Prema', 'Akrit', 'Suchitra', 'Abhimanu'],
        'Age': [20, 23, 41, 29, 32],
        'Marks': [92, 84, 35, 88, 83]}

df = pd.DataFrame(data)

def highlight_max(column):
    """Custom function to highlight maximum values"""
    is_max = column == column.max()
    return ['background-color: yellow; font-weight: bold' if cell else '' for cell in is_max]

# Apply custom highlighting
styled_df = df.style.apply(highlight_max)
print("Custom highlighting applied to maximum values")
Custom highlighting applied to maximum values

This approach allows you to customize the styling by modifying the CSS properties within the function. You can change colors, fonts, borders, or any other CSS attribute.

Highlighting Specific Columns

You can also highlight maximum values in specific columns only by using the subset parameter.

Example

import pandas as pd

# Create sample data
data = {'Name': ['Sai', 'Prema', 'Akrit', 'Suchitra', 'Abhimanu'],
        'Age': [20, 23, 41, 29, 32],
        'Marks': [92, 84, 35, 88, 83],
        'Score': [85, 90, 78, 95, 88]}

df = pd.DataFrame(data)

# Highlight maximum values only in numeric columns
styled_df = df.style.highlight_max(subset=['Age', 'Marks', 'Score'], color='lightgreen')
print("Maximum values highlighted only in Age, Marks, and Score columns")
Maximum values highlighted only in Age, Marks, and Score columns

Comparison of Methods

Method Ease of Use Customization Best For
highlight_max() Very Easy Limited Quick highlighting with basic styling
apply() with custom function Moderate Full Control Complex styling requirements
Subset parameter Easy Moderate Highlighting specific columns only

Conclusion

Use highlight_max() for quick and simple highlighting of maximum values in DataFrame columns. For advanced styling requirements, create custom functions with apply(). These methods enhance data visualization and make maximum values easily identifiable for better data analysis.

---
Updated on: 2026-03-27T07:44:33+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements