Style a dataframe
DataFrame output in the notebook can be personalised with some CSS syntaxe applied to the style
attribute.
Basic styling
Basic styling, color nan values in red, max values in blue for each Line and the min value in gold for each column.
# Import modules
import pandas as pd
# Example dataframe
raw_data = {'fruit': ['Banana', 'Orange', 'Apple', 'lemon', "lime", "plum"],
'color': ['yellow', 'orange', 'red', 'yellow', "green", "purple"],
'kcal': [89, 47, 52, 15, 30, 28],
'size_cm' : [20, 10, 9, 7, None, 4]
}
df = pd.DataFrame(raw_data, columns = ['fruit', 'color', 'kcal', "size_cm"])
(df.style
.highlight_null('red')
.highlight_max(color='steelblue', axis=0)
.highlight_min(color='gold',axis=1)
)
fruit | color | kcal | size_cm | |
---|---|---|---|---|
0 | Banana | yellow | 89 | 20 |
1 | Orange | orange | 47 | 10 |
2 | Apple | red | 52 | 9 |
3 | lemon | yellow | 15 | 7 |
4 | lime | green | 30 | nan |
5 | plum | purple | 28 | 4 |
Gradient
Color the value of the dataframe with a color gradient based on the value of the cell
raw_data = {'fruit': ['Banana', 'Orange', 'Apple', 'lemon', "lime", "plum"],
'color': ['yellow', 'orange', 'red', 'yellow', "green", "purple"],
'kcal': [89, 47, 52, 15, 30, 28],
'size_cm' : [20, 10, 9, 7, 6, 4]
}
df = pd.DataFrame(raw_data, columns = ['fruit', 'color', 'kcal', "size_cm"])
df.style.background_gradient()
fruit | color | kcal | size_cm | |
---|---|---|---|---|
0 | Banana | yellow | 89 | 20 |
1 | Orange | orange | 47 | 10 |
2 | Apple | red | 52 | 9 |
3 | lemon | yellow | 15 | 7 |
4 | lime | green | 30 | 6 |
5 | plum | purple | 28 | 4 |
Custom style
Create a custom style using CSS
def custom_style(val):
if val < 5:
return 'background-color:red'
elif val > 50:
return 'background-color:green'
elif abs(val) <20:
return 'background-color:yellow'
else:
return ''
df[["kcal", 'size_cm']].style.applymap(custom_style)
kcal | size_cm | |
---|---|---|
0 | 89 | 20 |
1 | 47 | 10 |
2 | 52 | 9 |
3 | 15 | 7 |
4 | 30 | 6 |
5 | 28 | 4 |
Colorbars
Draw bars in the cell based on the value in the cell
(df.style
.bar(subset=['kcal','size_cm'],color='steelblue')
)
fruit | color | kcal | size_cm | |
---|---|---|---|---|
0 | Banana | yellow | 89 | 20 |
1 | Orange | orange | 47 | 10 |
2 | Apple | red | 52 | 9 |
3 | lemon | yellow | 15 | 7 |
4 | lime | green | 30 | 6 |
5 | plum | purple | 28 | 4 |
import numpy as np
df["random"] = np.random.rand(6)-0.5
(df.style
.bar(subset=['kcal', 'size_cm'],color='steelblue')
.bar(subset=['random'],color=['indianred','limegreen'], align='mid')
)
fruit | color | kcal | size_cm | random | |
---|---|---|---|---|---|
0 | Banana | yellow | 89 | 20 | 0.432301 |
1 | Orange | orange | 47 | 10 | 0.104051 |
2 | Apple | red | 52 | 9 | 0.107809 |
3 | lemon | yellow | 15 | 7 | -0.281298 |
4 | lime | green | 30 | 6 | -0.122744 |
5 | plum | purple | 28 | 4 | 0.452038 |