Highlight the maximum value in last two columns in Pandas – Python


When working with data, it's often crucial to identify and highlight the maximum value within specific columns in a pandas dataframe. In Python, the Pandas library is widely used for data manipulation and offers efficient built-in functions.

This article focuses on highlighting the maximum value in the last two columns of a Pandas dataframe. By utilizing different methods, we can quickly locate and emphasize the highest values in our dataframe, which will facilitate easier analysis and comprehension of the dataset.

How to highlight the maximum value in the last two columns in Pandas?

To highlight the maximum value in the last two columns of a Pandas dataframe, we can employ different methods in Python. Below are the two approaches explained using code examples −

Method 1: Using the style property of a Pandas dataframe

One method to highlight the maximum value in the last two columns of a Pandas dataframe is by utilizing the style property. By accessing the style attribute of the dataframe, we can apply the highlight_max function, specifying the subset as the last two columns. This function automatically identifies the maximum value in each column and highlights it. The resulting styled dataframe visually emphasizes the highest values, facilitating easier analysis and comprehension of the data.

Example

import pandas as pd
# Create a sample dataframe
data = {'Column1': [10, 15, 8],
    'Column2': [20, 5, 12],
   'Column3': [7, 18, 9]}

df = pd.DataFrame(data)

# Highlight the maximum values in the last two columns
df_styled = df.style.highlight_max(subset=df.columns[-2:])

# Display the styled dataframe
df_styled

Output

This method utilizes the style property of a Pandas dataframe, specifically the highlight_max function. By passing the subset argument as df.columns[-2:], we specify the last two columns of the dataframe. This function highlights the maximum value in each column, making it visually distinct.

Method 2: Using conditional formatting

Another approach to highlight the maximum value in the last two columns of a Pandas dataframe is conditional formatting. We define a custom function, highlight_max, that compares each element in a series with the maximum value of that series. The function returns a list of style directives, indicating a yellow background for the maximum value. By applying this function using the apply method and specifying the subset as the last two columns, we achieve conditional formatting that highlights the highest values, aiding in data analysis and comprehension.

Example

import pandas as pd
# Create a sample dataframe
data = {'Column1': [10, 15, 8],
   'Column2': [20, 5, 12],
   'Column3': [7, 18, 9]}

df = pd.DataFrame(data)

# Define a function to highlight the maximum value
def highlight_max(s):
   is_max = s == s.max()
   return ['background-color: pink' if v else '' for v in is_max]

# Apply the function to the last two columns
df_styled = df.style.apply(highlight_max, subset=df.columns[-2:])

# Display the styled dataframe
df_styled

In this approach, we define a custom function called highlight_max, which compares each element in a series (s) with the maximum value of that series. The function returns a list of style directives, where the maximum value is highlighted with a yellow background. By using the apply function and specifying the subset as df.columns[-2:], we apply the conditional formatting only to the last two columns.

These methods provide different ways to highlight the maximum values in the last two columns of a Pandas dataframe. Choose the one that suits your needs and coding style.

Conclusion

In conclusion, highlighting the maximum value in the last two columns of a Pandas dataframe is a beneficial approach for comprehending and analyzing data. The employment of techniques such as style property or conditional formatting allows us to draw focus toward the most prominent data points.

Consequently, we can promptly recognize and scrutinize the critical data elements within the dataset. Whether by means of custom formatting or built-in functions, these methods provide effective techniques to emphasize the maximum values, thereby contributing towards an in-depth comprehension of the data and facilitating the process of data-driven decision-making.

Updated on: 24-Jul-2023

326 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements