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.
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.
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.
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.
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.
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.
Then execute the code. Observe the output in figure 10.
The data can be visualized as a bar chart using the style.bar() 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.
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.
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.
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.