Practical row formatting transforms raw data into meaningful, visually compelling presentations. This section explores concrete examples that demonstrate the power of custom styling techniques.
import pandas as pd
import numpy as np
def performance_styling(dataframe):
def highlight_performance(row):
styles = [''] * len(row)
if row['Performance Rating'] >= 4.5:
styles = ['background-color: #90EE90'] * len(row) ## Light Green
elif row['Performance Rating'] < 2.0:
styles = ['background-color: #FFB6C1'] * len(row) ## Light Red
return styles
df = dataframe.style.apply(highlight_performance, axis=1)
return df
## Example DataFrame
employee_data = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Department': ['Sales', 'Marketing', 'Engineering', 'HR'],
'Performance Rating': [4.7, 3.5, 2.0, 4.2]
})
styled_performance = performance_styling(employee_data)
2. Financial Data Visualization
graph TD
A[Raw Financial Data] --> B[Apply Conditional Formatting]
B --> C[Highlight Risk Levels]
C --> D[Styled Financial Report]
Scenario |
Styling Approach |
Key Characteristics |
Performance Review |
Color-based Ratings |
Visual Performance Indicators |
Financial Analysis |
Risk Level Highlighting |
Immediate Risk Perception |
Academic Grading |
Score-based Coloring |
Quick Performance Assessment |
3. Academic Grading Visualization
def grade_styling(dataframe):
def color_grades(row):
if row['Grade'] >= 90:
return ['background-color: green; color: white'] * len(row)
elif row['Grade'] >= 80:
return ['background-color: #90EE90'] * len(row)
elif row['Grade'] >= 70:
return ['background-color: yellow'] * len(row)
else:
return ['background-color: red; color: white'] * len(row)
return dataframe.style.apply(color_grades, axis=1)
## Student Grade Example
student_grades = pd.DataFrame({
'Name': ['Emma', 'Jack', 'Sophia', 'Michael'],
'Subject': ['Math', 'Physics', 'Chemistry', 'Biology'],
'Grade': [92, 78, 65, 88]
})
styled_grades = grade_styling(student_grades)
Combining Multiple Styling Rules
- Layer different styling conditions
- Create complex, multi-dimensional visualizations
- Enhance data interpretation
Best Practices
- Keep styling intuitive
- Maintain readability
- Use color meaningfully
- Test across different datasets
LabEx recommends experimenting with these practical formatting techniques to transform data presentation and improve insights.