Skip to content
SEO & Data Specialist | Arthur Camberlein
TwitterFRBlog FR

Background gradiant with pandas in Python

Python, Tips, Data1 min read

Did you know about .background_gradient()? Before today no!

Foggy forest as a background - Arthur Camberlein

I did not ... and now I do! And that thanks to a tweet from Karina Bartolomé and the screen capture below:

Background gradiant by Karina Bartolomé on Twitter - Arthur Camberlein All rights reserved to Karina Bartolomé for the screen capture

What is .background_gradient()?

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

Example from the tweet

If you want to copy/paste this coe, I reproduced it for you below:

1import pandas as pd
2
3df = pd.read_csv('https://github.com/datasciencedojo/datasets/blob/master/titanic.csv')
4
5(df
6 .groupby
7 .agg(
8 n = ('PassengerId','count')
9 survivors = ('Survived','sum')
10 p_survivors = ('Survived','mean')
11 )
12 .style
13 .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()

I saw this tip on Twitter and it was shared by Karina Bartolomé:

All rights reserved to Karina Bartolomé and her tip with .background_gradient() in her tweet.

© 2022 by SEO & Data Specialist | Arthur Camberlein. All rights reserved.
Theme by LekoArts