4.11. Style a DataFrame#

4.11.1. Highlight Your pandas DataFrame for Easier Analysis#

Have you ever wanted to highlight your pandas DataFrame for easier analysis? For example, you might want positive values in green and negative ones in red.

That could be done with df.style.apply.

import pandas as pd 

df = pd.DataFrame({"col1": [-5, -2, 1, 4], "col2": [2, 3, -1, 4]})
def highlight_number(row):
    return [
        "background-color: red; color: white"
        if cell <= 0
        else "background-color: green; color: white"
        for cell in row
    ]
df.style.apply(highlight_number)
  col1 col2
0 -5 2
1 -2 3
2 1 -1
3 4 4

One use case of highlighting is to compare the predictions of two or more models.

import pandas as pd

comparisons = pd.DataFrame(
    {
        "predictions_1": [1, 1, 1, 0, 1],
        "predictions_2": [0, 1, 0, 0, 0],
        "real_labels": [0, 1, 0, 0, 1],
    }
)


def highlight_cell(row):
    return [
        "background-color: red; color: white"
        if cell == 0
        else "background-color: green; color: white"
        for cell in row
    ]


comparisons.style.apply(highlight_cell)
  predictions_1 predictions_2 real_labels
0 1 0 0
1 1 1 1
2 1 0 0
3 0 0 0
4 1 0 1

4.11.2. Color the Background of a pandas DataFrame in a Gradient Style#

If you want to color the background of a pandas DataFrame in a gradient style, use df.style.background_gradient. The color of the cell will change based on its value.

import pandas as pd 

df = pd.DataFrame({"col1": [-5, -2, 1, 4], "col2": [2, 3, -1, 4]})
df.style.background_gradient()  
  col1 col2
0 -5 2
1 -2 3
2 1 -1
3 4 4
df.style.background_gradient(cmap='plasma')  
  col1 col2
0 -5 2
1 -2 3
2 1 -1
3 4 4

4.11.3. Format the Text Display Value of Cells#

Sometimes, you might want to format your DataFrame before writing it to a file such as an Excel sheet. df.style.format allows you to do that.

import pandas as pd  
import numpy as np 

df = pd.DataFrame({'item': ['a', 'b'], 'price': [np.nan, 2.34]})
s = df.style.format({'item': str.title,'price': '${:.1f}'}, na_rep='MISSING')
s 
  item price
0 A MISSING
1 B $2.3
s.to_excel("formatted_file.xlsx")

4.11.4. to_markdown: Print a DataFrame in Markdown Format#

Hide code cell content
!pip install tabulate 

Sometimes, you might want to include a table in a markdown, such as GitHub README. If you want to print a DataFrame in markdown format, use to_markdown().

import pandas as pd  

df = pd.DataFrame({'a': [1, 2, 3, 4], 'b': [5, 6, 7, 8]})
print(df.to_markdown())
|    |   a |   b |
|---:|----:|----:|
|  0 |   1 |   5 |
|  1 |   2 |   6 |
|  2 |   3 |   7 |
|  3 |   4 |   8 |

Copy and paste the output above in Jupyter Notebook’s markdown cell will give you an output like below:

a

b

0

1

5

1

2

6

2

3

7

3

4

8

You can also output markdown with a tabulate option:

print(df.to_markdown(tablefmt="grid"))
+----+-----+-----+
|    |   a |   b |
+====+=====+=====+
|  0 |   1 |   5 |
+----+-----+-----+
|  1 |   2 |   6 |
+----+-----+-----+
|  2 |   3 |   7 |
+----+-----+-----+
|  3 |   4 |   8 |
+----+-----+-----+