Updated On : Sep-03,2021 Tags pandas, styling
Simple Guide to Style Display of Pandas DataFrames

Simple Guide to Style Display of Pandas DataFrames

Pandas is the most commonly used data analysis tool in python for tabular data. It's built on the top of numpy and hence is quite fast in performing various data manipulation as well. Pandas data frames are commonly displayed in jupyter notebooks for presentation purposes as well. Pandas data frames are internally converted into HTML <table> and then displayed in Jupyter notebook. The default display is very simple and does not provide any special functionalities which can be used for presentation purposes like a different color of cells, rows, columns, etc. There can be instances when we want to use jupyter notebooks for presentation purposes and we want to highlight few things in our data frames to attract the attention of the audience towards them and we can do such things with pandas.

Pandas provide us with Styler object which has a bunch of methods that we can utilize to modify the default look of the data frames in Jupyter Notebook. It even lets us include CSS in our code to make attractive tables. The modifications will require a little bit of HTML and CSS knowledge to properly decorate tables. We can also take the HTML code of the modified dataframe and deploy it to our website. The Styler object is accessible through style attribute of a data frame.

We have created this tutorial to explain how we can style pandas data frame. We'll try to explain different methods with very simple examples so that it is easy to grasp. We'll start by importing the necessary libraries.

In [1]:
import pandas as pd
import numpy as np

We have started by creating a dataframe of 10 rows and 5 columns for explanation purposes. The data in the dataframe is random floats in the range 0-1. We'll be using this dataframe for all our examples to make things easier to understand.

In [2]:
data = np.random.random(size=(10,5))

df = pd.DataFrame(data=data, columns=list("ABCDE"),
                             index=["A1", "B2", "C3", "D4", "E5", "F6", "G7", "H8", "I9", "J10"])

df
Out[2]:
A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

Below we have retrieved Styler instance of the data frame. When we execute the styler instance in the jupyter notebook, it creates tabular representation from it as well because it has _repr_html_ method which creates an HTML representation of it.

In [3]:
print("Styler : {}".format(type(df.style)))

df.style
Styler : <class 'pandas.io.formats.style.Styler'>
Out[3]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

The Styler instStyle DataFrame Headerance has a method named render() which returns a string containing the HTML code of the data frame table and it includes CSS as well. Below we have printed only the first 1500 characters for explanation purposes.

In [4]:
print(df.style.render()[:1500])
<style type="text/css">
</style>
<table id="T_8d291_">
  <thead>
    <tr>
      <th class="blank level0" >&nbsp;</th>
      <th class="col_heading level0 col0" >A</th>
      <th class="col_heading level0 col1" >B</th>
      <th class="col_heading level0 col2" >C</th>
      <th class="col_heading level0 col3" >D</th>
      <th class="col_heading level0 col4" >E</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th id="T_8d291_level0_row0" class="row_heading level0 row0" >A1</th>
      <td id="T_8d291_row0_col0" class="data row0 col0" >0.074773</td>
      <td id="T_8d291_row0_col1" class="data row0 col1" >0.250925</td>
      <td id="T_8d291_row0_col2" class="data row0 col2" >0.280110</td>
      <td id="T_8d291_row0_col3" class="data row0 col3" >0.871257</td>
      <td id="T_8d291_row0_col4" class="data row0 col4" >0.192367</td>
    </tr>
    <tr>
      <th id="T_8d291_level0_row1" class="row_heading level0 row1" >B2</th>
      <td id="T_8d291_row1_col0" class="data row1 col0" >0.427473</td>
      <td id="T_8d291_row1_col1" class="data row1 col1" >0.915460</td>
      <td id="T_8d291_row1_col2" class="data row1 col2" >0.557301</td>
      <td id="T_8d291_row1_col3" class="data row1 col3" >0.325506</td>
      <td id="T_8d291_row1_col4" class="data row1 col4" >0.600302</td>
    </tr>
    <tr>
      <th id="T_8d291_level0_row2" class="row_heading level0 row2" >C3</th>
      <td id="T_8d291_row2_col0" class="data row2 col0" >0.474625</td>
      <td id="T_8d291_row2_col1" class="data

Style DataFrame Display Format

The Styler instance provides us with 4 useful methods which let us decorate HTML tables in three different ways. These 4 methods will do the working majority of the time. It does provide additional methods which we'll discuss as well but these four methods should do the working majority of the time.

  • 1. set_table_styles(table_styles,axis=0,overwrite=True) - This function is commonly used for styling of whole table instead of individual cells based on some conditions. This method accepts styling details for table as a list or a dictionary.
    • If we provide it with a list then an individual element of the list should be a dictionary with two keys (selector and props). We'll explain what should be the value of these keys soon.
    • If we provide it with a dictionary then the key of the dictionary can be individual column name or individual index value and the value of the dictionary is again dictionary with two keys (selector and props).
    • selector - This dictionary key accepts string value which corresponds to the table component that we want to decorate. It can be an HTML tag, the class value of an HTML tag, a combination of tag & class, and many other things which can be part of HTML that we can decorate.
    • props - This dictionary key accepts string value which has information about the actual decoration of component given to selector. It can be details like background color, font size, border color/size, padding, margin, etc. It can also accept decoration details as a list of tuples. We'll explain it below with examples to make things more clear.
    • The axis parameter is used in combination with table styles when styles are given as a dictionary. It accepts value 0/'index' to refer to applying style to each column or 1/'column' to apply styling to each row.
    • The overwrite parameter if set to True overwrites existing CSS and if set to False then it extends existing CSS.

Below is sample CSS data that we'll transform for the purpose of explanation. We have transformed it in two different ways.

CSS

thead {
    background-color:dodgerblue;
    color:white;
    border:3px solid red;
}

Style for Method 1

[
    {
        "selector":"thead",
        "props":"background-color:dodgerblue; color:white; border:3px solid red;"
    },

]

Style for Method 2

[
    {
        "selector":"thead",
        "props": [("background-color", "dodgerblue"), ("color", "white"),("border", "3px solid red"),]
    },

]
  • 2. set_td_classes(classes_dataframe) - This method works in combination with set_table_styles(). This method accepts a data frame that has the same size as that of the original data frame with values that are strings representing an HTML class that we want to assign to an individual cell. It'll assign that class to an individual cell of the HTML table and then we can apply style for those classes using set_table_styles(). This can be used to decorate individual cells of the data frame.

  • 3. apply(func,axis=0,subset=None) - This function is well suited for row-wise or column-wise decoration.

    • The func parameter takes as input callable function. The callable function should take as input Series and return a list of the same length with decoration details in it for each member of the Series.
    • The axis parameter works exactly like set_table_styles() method where 0 refers to each column (column-wise iteration) and 1 refers to each row (row-wise iteration).
    • The subset parameter accepts a list of strings specifying a subset of column or index values if we want to decorate only that subset of a table.
  • 4. applymap(func,subset=None) - This function is well suited for the decoration of individual cells based on some conditions.

    • The func parameter accepts a callable. The callable should take one value as input and return a string specifying decoration for that value. It'll decorate a cell containing that value based on the string returned from this function.

If you don't understand the methods from the theoretical explanation above then it's okay, don't worry. Things will become very easy when you actually see the results of applying them with few simple examples.

We'll now try to explain with simple examples how we can decorate different parts of the data frame table when displaying it in the jupyter notebook. We'll cover the below-mentioned TOPICS.

  • Style DataFrame Header
  • Style DataFrame Index and Header
  • Style Individual Column of DataFrame
  • Style Individual Row of DataFrame
  • Style Individual Cell of DataFrame (Not Recommended or Ideal Way)
  • Style Individual Cell of DataFrame (Efficient Way)
  • Styling Individual Cells of Selected Columns of DataFrame
  • Styling Individual Cells of Selected Rows of DataFrame
  • Styling Individual Cells at Intersection of Selected Rows and Columns of DataFrame
  • Styling Individual Cell of Row/Column By Iterating Row/Column at a Time
  • Highlight NaN/Null Values
  • Highlight Min/Max Values for Rows/Columns
  • Highlight Values in Particular Range
  • Highlight Quantile of Rows/Columns
  • Turn DataFrame into Heatmap (Apply Gradients)
  • Apply Style to All Cells
  • Bar Charts Inside DataFrame for Values of Rows/Columns

Style DataFrame Header

As a part of this section, we'll explain how we can style the header of the pandas data frame using set_table_styles() method. We have provided list with one dictionary to set_table_styles() method. We have set the value of selector key as thead which is HTML tag (header of HTML table) and the value of props with styling details like the background color of dodger blue, white font color, and border of 3 pixels.

We have also printed the type of returned object after calling method set_table_styles() which gets easily rendered by jupyter notebook.

In [5]:
s = df.style.set_table_styles([
                            {
                                "selector":"thead",
                                "props":"background-color:dodgerblue; color:white; border:3px solid red;"
                            },

                        ])

type(s)
Out[5]:
pandas.io.formats.style.Styler
In [6]:
s
Out[6]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

Below we have explained another way of decorating the header of the data frame where we give style details to props key as a list of tuples. We can notice that it produces the same results as the above one.

In [7]:
s = df.style.set_table_styles([
                            {
                                "selector":"thead",
                                "props": [("background-color", "dodgerblue"), ("color", "white"),
                                          ("border", "3px solid red"),
                                          ]
                            },
                        ])

s
Out[7]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

We can call render() method anytime to see the CSS and HTML of the table. We can export this to an HTML file and display it on some other website as well.

In [8]:
print(s.render()[:800])
<style type="text/css">
#T_fefdc_ thead {
  background-color: dodgerblue;
  color: white;
  border: 3px solid red;
}
</style>
<table id="T_fefdc_">
  <thead>
    <tr>
      <th class="blank level0" >&nbsp;</th>
      <th class="col_heading level0 col0" >A</th>
      <th class="col_heading level0 col1" >B</th>
      <th class="col_heading level0 col2" >C</th>
      <th class="col_heading level0 col3" >D</th>
      <th class="col_heading level0 col4" >E</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th id="T_fefdc_level0_row0" class="row_heading level0 row0" >A1</th>
      <td id="T_fefdc_row0_col0" class="data row0 col0" >0.074773</td>
      <td id="T_fefdc_row0_col1" class="data row0 col1" >0.250925</td>
      <td id="T_fefdc_row0_col2" class="data row0 col2" >0.280110</td>
      <td

Style DataFrame Index and Header

As a part of this section, we have explained how we can style the header and index of the data frame using set_table_styles() method. We have provided a method with a list of two dictionaries. The first dictionary like previous examples decorates the header whereas the second dictionary has decoration details for the index. We have used string th.row_heading as selector to decorate index. We can notice that th tags have details about an index of the data frame.

This time we have also increased the font size of both header and index. We have printed CSS and HTML of the table as well below to see styling details.

In [9]:
s = df.style.set_table_styles([
                            {
                                "selector":"thead",
                                "props": [("background-color", "dodgerblue"), ("color", "white"),
                                          ("border", "3px solid red"),
                                          ("font-size", "2rem"), ("font-style", "italic")]
                            },
                            {
                                "selector":"th.row_heading",
                                "props": [("background-color", "orange"), ("color", "green"),
                                          ("border", "3px solid black"),
                                          ("font-size", "2rem"), ("font-style", "italic")]
                            },
                        ])

s
Out[9]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400
In [10]:
print(s.render()[:800])
<style type="text/css">
#T_dc457_ thead {
  background-color: dodgerblue;
  color: white;
  border: 3px solid red;
  font-size: 2rem;
  font-style: italic;
}
#T_dc457_ th.row_heading {
  background-color: orange;
  color: green;
  border: 3px solid black;
  font-size: 2rem;
  font-style: italic;
}
</style>
<table id="T_dc457_">
  <thead>
    <tr>
      <th class="blank level0" >&nbsp;</th>
      <th class="col_heading level0 col0" >A</th>
      <th class="col_heading level0 col1" >B</th>
      <th class="col_heading level0 col2" >C</th>
      <th class="col_heading level0 col3" >D</th>
      <th class="col_heading level0 col4" >E</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th id="T_dc457_level0_row0" class="row_heading level0 row0" >A1</th>
      <td id="T_dc457_row0_col0" class="d

Style Individual Column of DataFrame

As a part of this section, we have explained how to target an individual column of the data frame. We have passed dictionary to set_table_styles() this time with key as C and value as list of one dictionary. The dictionary holds decoration details. It has selector set as td (HTML cell tag) and props set as a string of styling like a border of 2 pixels, green font color, and yellow background color.

In [11]:
s = df.style.set_table_styles({"C" : [
                                        {
                                            "selector" :"td",
                                            "props": "border: 2px solid red; color:green; background-color:yellow;"
                                        }
                                    ]
                              })

s
Out[11]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

Below we have explained the same example as above but this time we have passed decoration details as a list of tuples to props key. We have as usual printed CSS and HTML table details using render() method.

In [12]:
s = df.style.set_table_styles({"C" : [
                                        {
                                            "selector" :"td",
                                            "props": [("border","2px solid red"),
                                                      ("color", "green"),
                                                      ("background-color", "yellow")]
                                        }
                                    ]
                              })

s
Out[12]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400
In [13]:
print(s.render()[:500])
<style type="text/css">
#T_34294_ td.col2 {
  border: 2px solid red;
  color: green;
  background-color: yellow;
}
</style>
<table id="T_34294_">
  <thead>
    <tr>
      <th class="blank level0" >&nbsp;</th>
      <th class="col_heading level0 col0" >A</th>
      <th class="col_heading level0 col1" >B</th>
      <th class="col_heading level0 col2" >C</th>
      <th class="col_heading level0 col3" >D</th>
      <th class="col_heading level0 col4" >E</th>
    </tr>
  </thead>
  <tbody>
    <tr>

Style Individual Row of DataFrame

As a part of this section, we have explained how we can style individual rows of the data frames. We have passed a list of one dictionary to style the second row of a data frame. We can notice from the code of the HTML table above that row1 is the class for the second row of a table. We'll be giving that to selector to style it. The styling details are given to props key which has almost the same details as the previous example.

In [14]:
s = df.style.set_table_styles([
                                {
                                    "selector" :".row1",
                                    "props": [("border","2px solid red"),
                                              ("color", "green"),
                                              ("background-color", "yellow")]
                                }
                               ]
                             )

s
Out[14]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400
In [15]:
print(s.render()[:500])
<style type="text/css">
#T_9438a_ .row1 {
  border: 2px solid red;
  color: green;
  background-color: yellow;
}
</style>
<table id="T_9438a_">
  <thead>
    <tr>
      <th class="blank level0" >&nbsp;</th>
      <th class="col_heading level0 col0" >A</th>
      <th class="col_heading level0 col1" >B</th>
      <th class="col_heading level0 col2" >C</th>
      <th class="col_heading level0 col3" >D</th>
      <th class="col_heading level0 col4" >E</th>
    </tr>
  </thead>
  <tbody>
    <tr>

Below we have explained another way of highlighting a particular row. This time we have passed dictionary to set_table_styles() with key as value B2 and value as a list of a single dictionary that holds decoration details. We have set axis parameter to 1 which indicated that we'll be targeting each row and key B2 will limit targeting to only that row. The dictionary passed for decoration has selector set as td which will target all cells.

Please make a note that styling in this example does not style index value whereas in the previous example it did style index value as well.

In [16]:
s = df.style.set_table_styles({"B2" : [
                                        {
                                            "selector" :"td",
                                            "props": [("border","2px solid red"),
                                                      ("color", "green"),
                                                      ("background-color", "yellow")]
                                        }
                                    ]
                              },
                              axis=1
                             )

s
Out[16]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400
In [17]:
print(s.render()[:500])
<style type="text/css">
#T_d378c_ td.row1 {
  border: 2px solid red;
  color: green;
  background-color: yellow;
}
</style>
<table id="T_d378c_">
  <thead>
    <tr>
      <th class="blank level0" >&nbsp;</th>
      <th class="col_heading level0 col0" >A</th>
      <th class="col_heading level0 col1" >B</th>
      <th class="col_heading level0 col2" >C</th>
      <th class="col_heading level0 col3" >D</th>
      <th class="col_heading level0 col4" >E</th>
    </tr>
  </thead>
  <tbody>
    <tr>

As a part of this section, we have demonstrated how to style individual cells of the table. Though styling individual cell using set_table_styles() is possible but not recommended.

We have passed selector value of .row1.col1 which refers to a cell in the second row and second column. The value of props is like previous examples. We can notice here that we will need to pass different class names if we want to style individual cells which can increase the number of styling entries a lot and can make code cumbersome. Hence this way of styling individual cells is not recommended. We have explained the efficient way of styling individual cells in upcoming examples.

This method is okay if you want to style only a few cells of the data frame though.

In [18]:
s = df.style.set_table_styles([
                                    {
                                        "selector" :".row1.col1",
                                        "props": [("border","2px solid red"),
                                                  ("color", "green"),
                                                  ("background-color", "yellow")]
                                    }
                              ]
                            )

s
Out[18]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400
In [19]:
print(s.render()[:500])
<style type="text/css">
#T_26503_ .row1.col1 {
  border: 2px solid red;
  color: green;
  background-color: yellow;
}
</style>
<table id="T_26503_">
  <thead>
    <tr>
      <th class="blank level0" >&nbsp;</th>
      <th class="col_heading level0 col0" >A</th>
      <th class="col_heading level0 col1" >B</th>
      <th class="col_heading level0 col2" >C</th>
      <th class="col_heading level0 col3" >D</th>
      <th class="col_heading level0 col4" >E</th>
    </tr>
  </thead>
  <tbody>
    <tr

Style Individual Cell of DataFrame (Efficient Way)

As a part of this example, we have explained how to style individual cells of the data frame using set_td_classes() method.

As we had explained earlier, set_td_styles() works in combination with set_table_styles(). We'll be setting class for individual cells of data frame HTML table using set_td_styles() method and then decorate cells based on classes assigned using set_table_styles().

Below we have created a dataframe that has exactly the same shape as our original data frame. We have kept string green where the value in a cell is greater than 0.5 and red where the value is less than 0.5

In [20]:
data = np.array(["red" if cell < 0.5 else "green" for row in df.values for cell in row])

cell_color = pd.DataFrame(data.reshape(df.shape),
                          index=df.index,
                          columns=df.columns)

cell_color
Out[20]:
A B C D E
A1 red red red green red
B2 red green green red green
C3 red green green green green
D4 red green green red red
E5 green green green red green
F6 red red red green green
G7 green green red green green
H8 green green green green green
I9 red green green red green
J10 red green red green green

Below we have set the style for red classes to be of red background and for green classes to be of the green background. We have done that using set_table_styles() method. Then we have set classes using set_td_styles() method by giving it the data frame created in the previous step.

In [21]:
s = df.style.set_table_styles([
                            {"selector": ".green", "props": "background-color:lime; font-size:1.5rem;"},
                            {"selector": ".red", "props": "background-color:tomato; font-size:0.9rem;"}
                        ])

s.set_td_classes(cell_color)
Out[21]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

Below we have explained another way of styling individual cells of the data frame using applymap() method. As we had explained earlier, the applymap() method takes as input function which takes a single value as input and returns a single value as output. The output value is decoration detail and the single input value is cell data. It takes decisions based on the value of cell data. Like earlier, if a value is greater than 0.5 then it returns styling details with background color green and if a value is less than 0.5 then it returns styling details with background color red.

In [22]:
s = df.style.applymap(lambda x: "background-color:lime; font-size:1.5rem;" if x > 0.5 else "background-color:tomato; font-size:0.9rem;")

s
Out[22]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

Styling Individual Cells of Selected Columns of DataFrame

As a part of this section, we have explained how we can style individual cells of selected columns of the data frames. We have used applymap() method for this purpose. We have given a function that is the same as previous examples as the first parameter. We have set subset parameter to list of two columns names to target only those columns.

In [23]:
s = df.style.applymap(lambda x: "background-color:lime; font-size:1.5rem;" if x > 0.5 else "background-color:tomato; font-size:0.9rem;",
                      subset=["B", "D"])

s
Out[23]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

Below we have explained how we can decorate individual cells of selected columns using apply() method. It also takes as input function like applymap() method but input to that function is series and output is also series unlike applymap(). We have a set function that takes series as input and returns a list with styling details for each cell of that series. We have set subset parameter to two-column names to target cells of only those columns.

In [24]:
s = df.style.apply(lambda x: ["background-color:lime; font-size:1.5rem;" if i>0.5 \
                          else "background-color:tomato; font-size:0.9rem;" for i in x],
                   subset=["B", "D"],
                   )

s
Out[24]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

Styling Individual Cells of Selected Rows of DataFrame

As a part of this section, we have explained how we can style individual cells selected rows of the data frame. We have used apply() method for this purpose. We have set the function to the same function which we had used in our previous example. We have set subset parameter to a tuple of two values. The first value is a list of index names and the second value is a list of column names. We have given B2 and D4 as index names to decorate only those rows and for columns, we have given an empty list.

In [25]:
s = df.style.apply(lambda x: ["background-color:lime; font-size:1.5rem;" if i>0.5 \
                          else "background-color:tomato; font-size:0.9rem;" for i in x],
                   subset=(["B2", "D4"], slice(None)),
                   ) ## Axis=1 : Row and Axis=0: Column

s
Out[25]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

Styling Individual Cells at Intersection of Selected Rows and Columns of DataFrame

As a part of this section, we have explained how we can decorate cells that come at the intersection of selected rows and columns. We have used apply() function for this purpose.

We have given the same function to apply() method that we have used in the previous example. The subset parameter is set to a tuple with two lists where the first list has index names and the second list has column names.

In [26]:
s = df.style.apply(lambda x: ["background-color:lime; font-size:1.5rem;" if i>0.5 \
                          else "background-color:tomato; font-size:0.9rem;" for i in x],
                   subset=(["B2", "D4"], ["A","C"]),
                   ) ## Axis=1 : Row and Axis=0: Column

s
Out[26]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

Styling Individual Cell of Row/Column By Iterating Row/Column at a Time

As a part of this section, we'll explain how we can decorate the values of cells by going row at a time or column at a time. We have used apply() function for this purpose.

We have given function to apply() method which takes series as input and returns a list of the same length with details about styling for an individual element of series. The function sets background color as dodger blue if the value is a maximum value of series else sets the background color to orange.

We have set axis parameter to 1 to go row-wise. It'll pass a single row as a series to function.

In [27]:
s = df.style.apply(lambda x: ["background-color:dodgerblue;" if i == x.max() \
                              else "background-color:orange;" for i in x],
                   axis=1) ## Axis=1 : Row and Axis=0: Column

s
Out[27]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

Below we have explained how we can work on columns of the data frame using apply() function. We have passed the same function as our previous cell but this time we have set axis parameter to 0 which will send one by one column data as a series to function.

We can notice differences from the previous example. The previous example highlighted the maximum value per row and this one highlighted the maximum value per column.

In [28]:
s = df.style.apply(lambda x: ["background-color:dodgerblue;" if i == x.max() \
                              else "background-color:orange;" for i in x],
                   axis=0) ## Axis=1 : Row and Axis=0: Column

s
Out[28]:
  A B C D E
A1 0.074773 0.250925 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 0.325506 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 0.333313
E5 0.613465 0.504521 0.997631 0.393096 0.684118
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 0.048551 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 0.031775 0.960938
J10 0.499607 0.759137 0.284955 0.525883 0.909400

Highlight NaN/Null Values

As a part of this section, we'll explain how we can highlight null values in the data frame. We have first set few cells of the data frame as null. The Styler instance has a method named highlight_null() which can be used for special treatment of nulls.


  • highlight_null(null_color='red',subset=None,props=None) - This function highlights null values in data frame based on parameter. The null_color parameter accepts string specifying color name.
    • The props parameter accepts a string that can hold decoration details for null cells.
    • The subset parameter can be used to highlight nulls in particular columns.

Below we have highlighted null values as red color.

In [29]:
df.iloc[0, 1] = np.nan
df.iloc[1, 3] = np.nan
df.iloc[3, 4] = np.nan
df.iloc[4, 4] = np.nan
df.iloc[6, 2] = np.nan
df.iloc[8, 3] = np.nan
df.iloc[9, 4] = np.nan

df.style.highlight_null(null_color="tomato")
Out[29]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Below we have explained another example where we highlight null values by using props parameter.

In [30]:
 df.style.highlight_null(props="border: 2px solid dodgerblue; background-color: yellow; color:red;")
Out[30]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Below we have highlighted null values for only columns A,C, and E using subset parameter.

In [31]:
 df.style.highlight_null(props="border: 2px solid dodgerblue; background-color: yellow; color:red;",
                         subset=list("ACE"))
Out[31]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Highlight Min/Max Values for Rows/Columns

As a part of this section, we'll explain how we can highlight min/max values for each rows and columns of data frame. The Styler object provides highlight_min() and highlight_max() methods for this purpose.


  • highlight_min(subset=None,color='yellow',axis=0,props=None) -This method lets us highlight min per column/row based on axis parameter. If axis parameter is set to 0 then it'll highlight min per column and if its set to 1 then it'll highlight min per row.
    • The subset parameter can be used to target particular rows/columns.
    • The props parameter works like highlight_null() method.
  • highlight_max(subset=None,color='yellow',axis=0,props=None) - This method works exactly like highlight_min() but with only difference that it highlights maximum values.

Below we have highlighted min values per column with red color.

In [32]:
df.style.highlight_min(axis=0, color="tomato")
Out[32]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Below we have highlighted max values per column with red color.

In [33]:
df.style.highlight_max(axis=0, color="tomato")
Out[33]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Below we have highlighted min value per row using red color.

In [34]:
df.style.highlight_min(axis=1, color="tomato")
Out[34]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Below we have highlighted max value per row with red color.

In [35]:
df.style.highlight_max(axis=1, color="tomato")
Out[35]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Below we have highlighted min value per row with different styling details.

In [36]:
df.style.highlight_min(axis=1, props="border: 2px solid dodgerblue; background-color: yellow; color:red;")
Out[36]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Highlight Values in Particular Range

As a part of this section, we have explained how we can highlight values in a particular range using highlight_between() method of Styler instance.


  • highlight_between(left=None,right=None,color='yellow',axis=0,subset=None,inclusive='both',props=None) - This method can let us highlight values in particular range. The left and right parameter accepts scaler values and all values in data frame falling into that range will be included.
    • The inclusive parameter accepts 4 different string values. The value of 'both' will include values that are bounds of the range. The value of 'neither' will exclude both bounds. The value of 'left' will include left bound and exclude right bound. The value of 'right' will include right bound and exclude left bound.
    • The props parameter lets us include styling details as a string.
    • The axis and subset parameters work as it works in other methods that we explained earlier.

Below we have highlighted values in the range (0.3,0.6) with yellow color.

In [37]:
df.style.highlight_between(left=0.3, right=0.6, color="yellow", axis=0)
Out[37]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Below we have highlighted values in the range (0.3, 0.6) with different styling details.

In [38]:
df.style.highlight_between(left=0.3, right=0.6,
                           axis=1,
                           props="background-color:tomato; border:3px dotted dodgerblue;")
Out[38]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Highlight Quantile of Rows/Columns

As a part of this section, we have explained how we can highlight values in a particular quartile range using highlight_quartile() method of Styler object.


  • highlight_quartile(q_left=0, q_right=1,color='yellow',subset=None,axis=0,inclusive='both',props=None) - This method works exactly like highlight_between() method with only difference that it highlights value in particular quartile range. We can provide quartile range using q_left and q_right parameters.
    • Parameters color, subset, axis, inclusive and props works exactly like highlight_between() method.

below we have highlighted values in the quartile range of (0.0-0.5).

In [39]:
df.style.highlight_quantile(q_left=0.0, q_right=0.5,
                            axis=0, props="border: 2px solid grey; background-color: black; color:white;")
Out[39]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Below we have highlighted values in quartile range of (0.0-0.5) but does not include bounds.

In [40]:
df.style.highlight_quantile(q_left=0.0, q_right=0.5, inclusive="neither",
                            axis=0, props="border: 2px solid grey; background-color: black; color:white;")
Out[40]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Turn DataFrame into Heatmap (Apply Gradients)

As a part of this section, we have explained how we can create a heatmap in a data frame based on their values. The Styler instance provides 2 methods for this purpose.


  • background_gradient(cmap='PuBu',axis=0,subset=None) - This method lets us create heatmap based on data inside of data frame. It colors background of cells based on colormap given to it.The axis and subset parameters work as they do in other methods and can be used to create gradient only in some rows/columns.
  • text_gradient(cmap='PuBu',axis=0,subset=None) - This method works like background_gradient() but colors text and not background.

Below we have explained the usage of both methods on our data frame.

In [41]:
import matplotlib.pyplot as plt

df.style.background_gradient(cmap="Purples")
Out[41]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan
In [42]:
df.style.text_gradient(cmap="Purples")
Out[42]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Apply Style to All Cells

As a part of this section, we have explained how we can style all the cells of a data frame in one go. We can use this kind of styling when we don't have to do conditional styling. Styling this way does not style the header and index, it only styles all cells. The Styler instance provides a method named set_properties() for this purpose.


  • set_properties(subset=None,**kwargs) - This method lets us give styling details as parameter of it. We can give as many values as we want and the style details specified by those parameters and their values will be applied to all cells of a data frame. We can also use subset parameter as we had with earlier methods to decorate only particular rows/columns of a data frame.

Below we have explained the usage of the method with simple examples.

In [43]:
df.style.set_properties(color="red", border="2px solid dodgerblue")
Out[43]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan
In [44]:
df.style.set_properties(**{
                            "color":"red",
                            "border":"2px solid dodgerblue",
                            "background-color": "yellow",

                           })
Out[44]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Bar Charts Inside DataFrame for Values of Rows/Columns

As a part of this section, we have explained how we can bar inside of pandas data frame which has only float or integer values inside of it. The Styler instance provides us with a method named bar() which lets us create a bar chart inside of the data frame.


  • bar(subset=None,axis=0,color='#d65f5f',width=100, align='left') - This method can create bar inside of pandas data frame based on the value of that cell. We can provide color details as single color or two colors when values are combination of positive and negative.
    • The align parameter accepts one of the three values ('left','zero' and 'mid'). The 'left' will make sure that the minimum value starts at the left of the cell. The 'zero' value makes sure that zero is located at the center of the cell. The 'mid' value makes sure that the center of the cell has (max-min)/2 value.
    • The axis parameter works like other methods we had explained above.

NOTE

Please make a note that when method is going column-wise, it'll only consider values of that columns to create bar chart and not the values of whole data frame. The same will happen when it goes row-wise. Pay attention to axis parameter for this.

Below we have simply created a bar chart with lime color on our existing data frame.

In [45]:
df.style.bar(color='lime')
Out[45]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Below we have given two colors as input and aligned bar in the center.

In [46]:
df.style.bar(color=['tomato','lime'],align='mid')
Out[46]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Below we have explained another example that has the same code as the previous one but this time we have set axis parameter to 1 so that it goes row by row instead of column by column.

In [47]:
df.style.bar(color=['tomato','lime'],align='mid', axis=1)
Out[47]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 0.427473 0.915460 0.557301 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 0.673604 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 0.291388 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 0.915084 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 0.818678 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Below we have set few values in the data frame as negative and then created a bar chart on it by setting align parameter to zero. This will make sure that 0 is in the center.

In [48]:
df.iloc[5, 0] = -0.5
df.iloc[7, 0] = -0.7
df.iloc[1, 0] = -0.1
df.iloc[3, 2] = -0.2
df.iloc[1, 2] = -0.1
df.iloc[8, 2] = -0.9

df.style.bar(color=["tomato", "lime"], align="zero")
Out[48]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 -0.100000 0.915460 -0.100000 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 -0.200000 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 -0.500000 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 -0.700000 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 -0.900000 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

Our last example explains how we can apply this to only a few columns/rows based on our needs.

In [49]:
df.style.bar(color=["tomato", "lime"], subset=["A", "C"], align="mid")
Out[49]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 -0.100000 0.915460 -0.100000 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 -0.200000 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 -0.500000 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 -0.700000 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 -0.900000 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan
In [50]:
df.style.bar(color=["tomato", "lime"], subset=(["B2", "F6"],slice(None)), axis=0, align="zero")
Out[50]:
  A B C D E
A1 0.074773 nan 0.280110 0.871257 0.192367
B2 -0.100000 0.915460 -0.100000 nan 0.600302
C3 0.474625 0.548117 0.854918 0.914851 0.686006
D4 0.392461 0.970572 -0.200000 0.308940 nan
E5 0.613465 0.504521 0.997631 0.393096 nan
F6 -0.500000 0.498140 0.255995 0.607597 0.931598
G7 0.836272 0.591719 nan 0.811875 0.680160
H8 -0.700000 0.586229 0.985577 0.848604 0.965530
I9 0.410924 0.849381 -0.900000 nan 0.960938
J10 0.499607 0.759137 0.284955 0.525883 nan

This ends our small tutorial explaining how we can style pandas data frames with CSS styles. Please feel free to let us know your views in the comments section.

References



Sunny Solanki  Sunny Solanki