How to Improve Your Analytical Report With Conditional Formatting In Pandas
Last Updated on July 3, 2022 by Editorial Team
Author(s): Hrishikesh Patel
Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.
How To Improve Your Analytical Report With Conditional Formatting InΒ Pandas
Excel-like Conditional Formatting in Pandas Using StylerΒ API
Clearly communicating analytical insights with stakeholders is crucial for data scientists/analysts.
The use of conditional formatting in analytical reports can help in quickly identifying insights into a data frameΒ (table).
Letβs start with an example first. The following pivot table shows the total sales of different products from 2016 toΒ 2022.
- Can you identify the largest selling π° product in 2016?βββYes it is Product_B with a total sale of 169 but itβs difficult to identify just by lookingπ at theΒ table.
Now letβs color the largest sellingπ° product for each year. After highlighting, it becomes so much easier to answer the above question, isnβtΒ it?
Let me show you how to do this inΒ Pandas.
Topics:
1. Highlight missing values
2. Highlight the maximum (or minimum) value in each row/column
3. Highlight values within a range
4. Plot in-column bar chart
5. Highlight values using a color gradient
6.Β Bonusπ
Note: I strongly recommend using the latest version of Pandas. You can run pip install –upgrade pandas to get Pandasβ latest stableΒ release.
1. Highlight missingΒ values
Using dataframe.style.highlight_null() you can color null values as shown below. I stored the pivot table in the variable df_pivotedΒ .
Itβs okayπ if you donβt prefer red. Letβs customize the text and background color of missing values using the argument props=βcolor:white;background-color:blackβΒ .
After highlighting, we can quickly get the insight that Product_H was not sold inΒ 2018.
2. Highlight maximum (or minimum)Β values
To highlight maximum values in each column, you can use dataframe.style.highlight_max()Β . The method by default colors maximum values in each column as illustrated in the belowΒ image.
To color max values in each row, you can specify the argument axis=1Β .
Note: Similarly you can use the method dataframe.style.highlight_min() with proper arguments to color minimum values in rows/columns.
3. Highlight values within aΒ range
Letβs consider that we want to highlight values between 100 and 200βββitβs quite easy to using dataframe.style.highlight_between(left, right)Β .
4. Plot in-column barΒ chart
A bar chart plotted within a column can be visually appealing and useful. Such bar charts can be created using dataframe.style.bar() the method as shownΒ below.
Letβs customize the bar chart to change its color andΒ size.
5. Highlight values using a colorΒ gradient
What if you want to highlight the entire column with a color gradient. It can be done using dataframe.style.background_gradient() as depicted below. In the image, the color changes from red to green as the value increases. You can set subset=None to apply the gradient to the entire dataΒ frame.
6. BonusΒ π
How can we highlight min, max, and missing values together in the data? Well, you can define a function as illustrated below. The function highlights min, max, and nan values in the column βProduct_Cβ. By setting subset=NoneΒ , it highlights the values in the entire data frame. Isnβt this function really cool? Let me know your thoughts in the comments!
Please feel free to explore highlighting methods in Pandas documentation.
Before youΒ go!
I hope you have enjoyed the story and found it useful. Follow me on Medium if youβd like more stories like this and subscribe to me to get my new stories directly into yourΒ inbox.
My other stories you mightΒ enjoyβ¦
- Try These Pandas Display Configurations in Your Next Analysis
- Regular Expression (RegEx) in PythonΒ : The Basics
How to Improve Your Analytical Report With Conditional Formatting In Pandas was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.
Join thousands of data leaders on the AI newsletter. Itβs free, we donβt spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI