Background gradiant with pandas in Python
— Python, Tips, Data — 1 min read
Did you know about
.background_gradient()? Before today no, I did not!
I did not ... and now I do! And that thanks to a tweet from Karina Bartolomé and the screen capture below:
All rights reserved to Karina Bartolomé for the screen capture
It is a styling format from the library
pandas in Python.
If you want to know more and read the documentation, you can follow this link https://pandas.pydata.org/docs/reference/api/pandas.io.formats.style.Styler.background_gradient.html
Pandas is one of the library I recommend you to install on my article How to import python libraries
Example from the tweet
If you want to copy/paste this coe, I reproduced it for you below:
1import pandas as pd23df = pd.read_csv('https://github.com/datasciencedojo/datasets/blob/master/titanic.csv')45(df6 .groupby7 .agg(8 n = ('PassengerId','count')9 survivors = ('Survived','sum')10 p_survivors = ('Survived','mean')11 )12 .style13 .background_gradient(subset=['p_survivors'], vmin=0, vmax=1, cmap='colorwarn_r')14 .background_gradient(subset=['n','survivors'], cmap='blues')15)
For the purpose of this example, it was done using a Python Notebook
Source of this tip on
.background_gradient() is Twitter
I saw this tip on Twitter and it was shared by Karina Bartolomé:
.background_gradient() pic.twitter.com/nUhdhkv6yf— Karina Bartolomé (@karbartolome) July 1, 2022
All rights reserved to Karina Bartolomé and her tip with
.background_gradient() in her tweet.