Styling increase the redness of the data. If you have previous experience in styling your excel sheet then you must be thinking of how to do styling in Pandas Dataframe in Python. In pandas also, You can do conditional formatting of the data values and style them easily. In this entire post, you will learn to style dataframe elements using Pandas through conditional formatting. You will learn the following things
How to do conditional Formatting for the entire DataFrame?
Changing the Background color for the max value in Column.
But before doing the styling of the dataframe let’s import necessary libraries and make a Pandas Dataframe. Use the following code.
import pandas as pd import numpy as np np.random.seed(24) df = pd.DataFrame(np.random.randn(10, 5), columns=list('ABCDE')) df.iloc[2, 4] = np.nan df
Here I am creating columns A, B, C, D with random values inside them. The total number of elements is 50 that is 10 rows and 5 columns.
Conditional Formatting for Entire DataFrame
Suppose I want to color the elements according to their sign negative or positive. I want to color the negative value with red and the positive one with a green color. To do so I have created a function named color_elements(val) with parameter val for accepting all the elements of the Dataframe. Below is the function definition.
def color_elements(val): color = 'red' if val < 0 else 'green' return 'color: %s' % color
After that, you will use the applymap() function for coloring all the elements according to the condition.
s = df.style.applymap(color_negative_red) s
Columns Wise Formatting of the DataFrame
You can also do styling columns wise. For example, I want to change the background color of the value that is maximum in the column. I have first created a function for this.
def color_max(s): max = s == s.max() return ['background-color: yellow' if i else '' for i in max]
Here Inside the function first I am comparing all the values with the column’s maximum and assign true or false to the max variable. Thus max will be a list of all the boolean values and only changes the background color that has a maximum value. The output is below.
You can also apply all the styling conditions as a pipeline. I first want to color the negative values and then changing the background color. Use the following code.
df.style.\ applymap(color_negative_red).\ apply(highlight_max)
Just like you do color in conditional formatting in excel sheet. You can also do formatting in Pandas. But currently, this feature can be done in Jupyter Notebook Only. You cannot get the same output in Pycharm. If you want to know more about it then you can read about it in Pandas Offical Documentation. If you have any queries about it then you can also contact us for more information.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.