Highlight the minimum value in each column In Pandas

Pandas provides several methods to highlight the minimum value in each column of a DataFrame. This technique is useful for outlier identification, detecting data quality issues, and exploring data distribution patterns.

In this article, we will explore three effective methods to highlight minimum values using Pandas styling functions and visualization techniques.

Method 1: Using style.highlight_min()

The style.highlight_min() method provides the simplest approach to highlight minimum values. It applies a yellow background to the minimum value in each column by default.

Example

import pandas as pd

# Create a sample DataFrame
data = {'A': [10, 20, 30],
        'B': [5, 15, 25], 
        'C': [40, 50, 35]}
df = pd.DataFrame(data)

print("Original DataFrame:")
print(df)
print("\nHighlighted DataFrame (minimum values):")

# Highlight minimum values in each column
highlighted_df = df.style.highlight_min()
highlighted_df
Original DataFrame:
    A   B   C
0  10   5  40
1  20  15  50
2  30  25  35

Highlighted DataFrame (minimum values):
(Displays DataFrame with yellow highlighting on minimum values: A=10, B=5, C=35)

Method 2: Using Custom Function with style.apply()

The style.apply() method allows you to create custom highlighting functions with more control over styling options.

Example

import pandas as pd

# Create a sample DataFrame
data = {'A': [10, 20, 30],
        'B': [5, 15, 25],
        'C': [40, 50, 35]}
df = pd.DataFrame(data)

# Define a custom function to highlight minimum values
def highlight_min(column):
    min_val = column.min()
    return ['background-color: lightcoral' if val == min_val else '' for val in column]

print("Original DataFrame:")
print(df)
print("\nCustom highlighted DataFrame:")

# Apply the custom function
highlighted_df = df.style.apply(highlight_min)
highlighted_df
Original DataFrame:
    A   B   C
0  10   5  40
1  20  15  50
2  30  25  35

Custom highlighted DataFrame:
(Displays DataFrame with light coral highlighting on minimum values)

Method 3: Using style.background_gradient()

The style.background_gradient() method applies a color gradient across the entire DataFrame, making minimum values visually distinct through color intensity.

Example

import pandas as pd

# Create a sample DataFrame
data = {'A': [10, 20, 30],
        'B': [5, 15, 25],
        'C': [40, 50, 35]}
df = pd.DataFrame(data)

print("Original DataFrame:")
print(df)
print("\nGradient highlighted DataFrame:")

# Apply gradient highlighting
highlighted_df = df.style.background_gradient(cmap='YlOrRd')
highlighted_df
Original DataFrame:
    A   B   C
0  10   5  40
1  20  15  50
2  30  25  35

Gradient highlighted DataFrame:
(Displays DataFrame with color gradient - lighter colors for minimum values, darker for maximum)

Comparison of Methods

Method Customization Best For Visual Style
highlight_min() Limited Quick identification Yellow background
style.apply() High Custom styling needs User-defined colors
background_gradient() Medium Value distribution analysis Color gradient

Conclusion

Use style.highlight_min() for quick minimum value identification. Choose style.apply() with custom functions for specific styling requirements. Use background_gradient() when you need to visualize the overall data distribution pattern.

Updated on: 2026-03-27T07:47:19+05:30

699 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements