Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Method 1: Using the style.highlight_max() Function
The simplest approach is to use the built-in highlight_max() function with the subset parameter to target only the last two columns ?
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)
print("Original DataFrame:")
print(df)
# Highlight the maximum values in the last two columns
df_styled = df.style.highlight_max(subset=df.columns[-2:])
print("\nDataFrame with highlighted maximum values in last two columns:")
df_styled
Original DataFrame: Column1 Column2 Column3 0 10 20 7 1 15 5 18 2 8 12 9
This method utilizes the style property of a Pandas dataframe, specifically the highlight_max() function. By passing subset=df.columns[-2:], we specify the last two columns of the dataframe. This function highlights the maximum value in each column with a yellow background.
Method 2: Using Custom Conditional Formatting
For more control over the styling, you can create a custom function that applies specific formatting to maximum values ?
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(series):
is_max = series == series.max()
return ['background-color: lightcoral' 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:])
print("DataFrame with custom highlighting:")
df_styled
In this approach, we define a custom function called highlight_max, which compares each element in a series with the maximum value of that series. The function returns a list of style directives, where the maximum value is highlighted with a light coral background. By using the apply() function and specifying subset=df.columns[-2:], we apply the conditional formatting only to the last two columns.
Method 3: Highlighting with Different Colors
You can also highlight maximum values with different colors for each column ?
import pandas as pd
# Create a sample dataframe
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Score1': [85, 92, 78],
'Score2': [90, 88, 95]}
df = pd.DataFrame(data)
# Highlight max in last two columns with different colors
df_styled = df.style.highlight_max(subset=['Score1'], color='lightblue') \
.highlight_max(subset=['Score2'], color='lightgreen')
print("DataFrame with different colors for each column:")
df_styled
Key Points
-
df.columns[-2:]selects the last two columns of the dataframe -
highlight_max()automatically finds and highlights maximum values - Custom functions provide more control over styling options
- You can chain multiple styling operations for different effects
Conclusion
Highlighting maximum values in the last two columns helps quickly identify peak performance or important data points. Use highlight_max() for simple highlighting or create custom functions for more advanced styling control.
