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 Pandas DataFrame\'s Specific Columns using Apply()
While presenting or explaining data using Pandas DataFrames, we might need to highlight important rows and columns to make them more appealing, explainable and visually stunning. One way of highlighting specific columns is by using the built-in apply() method with Pandas styling.
Understanding apply() with Pandas Styling
The apply() method is used to apply a user-defined function to each column or row of the Pandas DataFrame. To highlight specific columns, we first define a custom function that sets the required conditions for highlighting, then use the apply() method along with the style module.
Syntax
df.style.apply(function_name)
Highlighting Specific Columns
Let's start with a basic example of highlighting specific columns by name ?
import pandas as pd
# Create a sample DataFrame
data = {
'Name': ['Ram', 'Shyam', 'Mohan', 'Shrey'],
'Age': [25, 30, 35, 40],
'Score': [80, 90, 85, 95]
}
df = pd.DataFrame(data)
# Function to highlight specific columns
def highlight_columns(col):
color = 'lightblue' if col.name in ['Age', 'Score'] else 'white'
return ['background-color: {}'.format(color) for _ in col]
# Apply highlighting
styled_df = df.style.apply(highlight_columns)
print("DataFrame with highlighted columns created successfully!")
print(df)
DataFrame with highlighted columns created successfully!
Name Age Score
0 Ram 25 80
1 Shyam 30 90
2 Mohan 35 85
3 Shrey 40 95
Highlighting All Columns
To highlight all columns with the same color, we can modify our function ?
import pandas as pd
data = {
'Name': ['Ram', 'Shyam', 'Mohan', 'Shrey'],
'Age': [25, 30, 35, 40],
'Score': [80, 90, 85, 95]
}
df = pd.DataFrame(data)
# Function to highlight all columns
def highlight_all_columns(col):
color = 'lightgreen'
return ['background-color: {}'.format(color) for _ in col]
styled_df = df.style.apply(highlight_all_columns)
print("All columns highlighted with light green!")
print(df)
All columns highlighted with light green!
Name Age Score
0 Ram 25 80
1 Shyam 30 90
2 Mohan 35 85
3 Shrey 40 95
Conditional Highlighting
We can also highlight specific values within columns based on conditions ?
import pandas as pd
data = {
'Name': ['Ram', 'Shyam', 'Mohan', 'Shrey'],
'Age': [25, 30, 35, 40],
'Score': [80, 90, 85, 95]
}
df = pd.DataFrame(data)
# Function for conditional highlighting
def highlight_conditions(col):
if col.name == 'Age':
return ['background-color: lightblue' if val > 30 else '' for val in col]
elif col.name == 'Score':
return ['background-color: lightgreen' if val >= 90 else '' for val in col]
else:
return ['' for _ in col]
styled_df = df.style.apply(highlight_conditions)
print("Conditional highlighting applied:")
print("- Age > 30: Light blue background")
print("- Score >= 90: Light green background")
print(df)
Conditional highlighting applied:
- Age > 30: Light blue background
- Score >= 90: Light green background
Name Age Score
0 Ram 25 80
1 Shyam 30 90
2 Mohan 35 85
3 Shrey 40 95
Key Features
| Method Type | Use Case | Example |
|---|---|---|
| Column-based | Highlight entire columns | col.name in ['Age', 'Score'] |
| Value-based | Highlight specific values | val > 30 |
| Universal | Highlight all columns | Same color for all |
Conclusion
The apply() method with Pandas styling provides a powerful way to highlight DataFrame columns based on various conditions. Use it to make your data presentations more visually appealing and easier to understand.
