how to apply styles to pandas dataframe

In this session we are going to look at how to style a pandas data frame. That means, in order to know what are the maximum or minimum value of a column, those columns can be highlighted using few colors. This is advantageous when analyzing data. We will be defining few functions in this session.

First open a Jupyter notebook and import Pandas and the relevant data file as shown in the figure 1.

Figure 1: Importing the data

We are going to define a function such that, the values are going to be highlighted in blue if the values are less than 1300, and in black otherwise. The function definition is shown in figure 2. Then the style.applymap() function should be applied to our data frame; stock_data. The argument to the style.applymap() function should be the value_colour_blue function that we defined.

Figure 2: The function definition

Then execute the data frame and observe the output as shown in figure 3.There are two values highlighted in blue which are lesser than 1300.

Figure 3: The output showing values in blue if lower than 1300

As an example, let’s highlight the values greater than 1153706 in blue as shown in figure 4. This can be done by simply changing the operator and the value inside the function definition. St data_set>1153706Then execute the code. Note that many values are blue. Those values are greater than the given value.


Now let’s define a function that will take the data frame (s) as the input and get the minimum value which will be highlighted in yellow. Then apply it using the style.apply() function as shown in figure 5.

Figure 5: The function to highlight the minimum values in yellow

Execute the function. The output is shown in figure 6. It can be seen that the minimum values of each column are highlighted in yellow.

Figure 6: Minimum values are highlighted in yellow

Let’s see how we can highlight the maximum values. This can be done in the same way. But, the s.min() should be changed as s.max() as we are going to select the maximum values. The function definition is shown in figure 7 and here we are going to highlight the values in orange color.


Execute the code and observe the result in figure 8. We can see that the maximum value in each column is highlighted in orange color.


Then let’s see how the minimum value, maximum value, and values lower than 1300 can be highlighted. This can be done by applying all the functions that we defined earlier. The code is shown in figure 9.

Figure 9: Code for highlighting min, max and values lower than 1300

Then execute the code. Observe the output in figure 10.


The data can be visualized as a bar chart using the function as shown in figure 11. We are going to visualize this bar chart for High and Open columns. So, specify those two columns inside the function under subset parameter and give middle for the align parameter.

Figure 11: Applying a bar chart inside the columns

Execute the above code. Observe the figure 12. Two bar charts are plotted for the Open and High columns by considering their data. As an example, 1590 is the maximum value for the Open column. The bar is completely colored for that value.

Figure 12: The bar charts

Assume that we also need a bar chart for the Volume column. Simply pass Volume into the subset parameter as shown in figure 13 and execute the code. Now, the Volume column also has a bar chart.

Figure 13: Adding a bar chart to the Volume column

In order to highlight the null values, style.highlight() function can be used. Give the color that you want the null values to be highlight for the null_color parameter. Then execute. The figure 14 shows the output which the null values are highlighted in Red color.

Figure 14: Highlighting the null values in red

Video Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *