How to Adjust the Style of Pandas DataFrame
Skills:
ML Pipelines60%
Key Takeaways
This video demonstrates how to adjust the style of Pandas DataFrame using CSS styling, including changing header colors, row colors, and hover highlight colors, with tools such as pandas, numpy, and google collab.
Full Transcript
welcome back to the data professor YouTube channel if you new here my name is Tim in Nanta Sanomat and I'm an associate professor of bioinformatics on this YouTube channel we cover about data science concepts and practical tutorials if you're into this type of content please consider subscribing are you using pandas for your data science project are you bored of the same old styling of the pandas dataframe if you answered yes to both of these then you're gonna be happy with what is in store for you today so in this video I'm going to show you how you can adjust the style of the pandas dataframe to a customized color of your choice so without further ado let's get started so the first thing that you want to do is head over to the github of the data professor and click on the code repository and then scroll down click on Python down find Panda dialing data frame click on that okay and right-click on the raw link save the gas and save it into your computer if you have a google collab you can follow along as well by clicking on the file open notebook and then click on the github tab type in data professor and find panda styling data frame and click on it okay but because I already have it open I'm going to use this version and so let me start by clearing all outputs before we begin so that we can start together all right so let's begin and so the first step here is to import the necessary libraries which is numpy and pandas okay and then the next step is we're going to download the NBA player stats data for our example and as you might recall this was collected by scraping from the website in a previous video so please find the link to the video in the video description down below and also in the link up here okay let's continue okay and so we're gonna download this from our github and then we're going to read in the panda's data frame and we're gonna display selected columns as shown here in the selection variable and then to make the selection into the data frame we're going to define the f equals to DF open and closing bracket and inside as the argument will be selection and so we're going to display only the selected columns here not the entire data set okay so let's have a look how does it look like and so this is the typical data frame that you're gonna see when you're using Google collab or when you're using your tubular notebook or even your aqua notebook so it's going to have this plane look okay not that it's not good but it is a little bit boring so let's say that you want to color this to something else can you do it well yeah so I'm gonna show you and so the styling trick of the pandas dataframe is brought to you by this article in towards data science and so if you would like to see other tips from this please have a look at this article and so here we're gonna show you selected tips in styling the pandas table or the pandas dataframe with CSS styling and so let's look at the code so we're gonna start by defining the data frame DF and then we're gonna show only the first 20 rolls so we're gonna have the head function and inside the argument we're gonna be 20 so that will display 20 rolls and then it's gonna be dot style dot and then we're gonna use the set table Styles function and as argument it's going to be right here as highlighted here and so here we're gonna select the background color of the header row and this is going to be the font color which is going to be white and the background color will be green and here we're gonna display the font using verdana and then we're gonna display the background color of the odd rows and even rolls differently so in the odd rows we're gonna have it in the gray color but in the even rows we're gonna show it as a white color and then we're gonna hide the index number from the display okay so let's run this code alright so this is the before okay and this is the after okay so you can color this any other color that you like okay but in this example I'm using green color and you're gonna see here that the odd number is gray dcdcdc which is the hex code for grey color and you see here that we also use the common name as well white so you can use the common name as well as the caps code to define the color okay so the odd rows are gray even rows are white okay so this might help you to quickly scan the table alright so actually the default also has light grey color okay and the difference here is that in the collab you have this over highlight as well meaning that when your mouse hovers over a particular roll you see that the background color becomes darker gray color okay but in this style nothing happens but I'm gonna show you in just a moment how you can make that highlight in a different color okay and so let's move on alright and here we're gonna add the yellow highlight color so the only different here is this block of code so this will tell us that upon hovering the mouse the background color will be changed to yellow so it's gonna be the background color of the particular role that the mouse hovers over okay so let's have a look so when we hover the mouse over the particular roll it changes the background color to be yellow okay so this might be more clearer than the default color of darker gray okay so you can change this to other color that you like if you make it read right could it be pink okay it could be pinkish color as well oh hi MA purple okay maybe too dark okay so let's say that you want to have other colors you can head over to this link that I provided at the bottom of this notebook so select a color that you like maybe you want some orange looking color right maybe this is orange so it could be any gradient color any that you like here let's it I like this color I'll just copy this tax code and I'm going to paste it here and remember to add the hashtag or the pound key and there you go or is color okay so let's say that you want to perform some conditional coloring in this example I'm gonna show you how you can color the values of a particular column based on conditions so here we have three scenarios right if the value is less than five it will be colored black if the value is greater than five but less than twenty it will be colored red and if the value is greater than twenty it will be colored green okay so whenever each of this condition returns to be true then it will return this statement and this statement will output color : and then it's gonna make use of the modulo operator meaning that the color variable here will be replaced with the respective string of the color either green red or black and so this value will be placed in here and so it will be color colon and then the name of the color that we want inside here okay and so in this example we're gonna use the points column and so let's have a look and so you're gonna see that the color of value squinted at twenty will be green and if it's greater than five it'll be red but less than twenty and other values less than five will be black okay so this might come in handy when you are eyeballing the data set and you just want a quick glance of the relative distribution of the values so based on your own binning the three color binning the green red and black frequency of each of these three classes okay and so make note that the index number is also shown here and so if you see that values having green color it has a index number of seven and index number of nineteen and so you could also display the player name the position and the team okay also for the first twenty row and then you're gonna see the index number right here so if it's green its number nineteen and number seven right so number seven is right here and number nineteen okay just in case you would like to know the name alright so this will allow you to quickly have a look all right and so this is a quick tips and tricks on how you can style your pandas dataframe and if this video was helpful to you please give it a thumbs up and subscribe if you haven't yet done so and as always the best way to learn data science is to do data science and so please enjoy the journey thank you for watching please like subscribe and share and I'll see you in the next one but in the meantime please check out these videos
Original Description
Are you bored of the default style of the Pandas DataFrame? In this video, I will show you how to adjust the looks (formatting styles) of the Pandas DataFrame (i.e. changing header colors, row colors and hover highlight colors).
🌟 Buy me a coffee: https://www.buymeacoffee.com/dataprofessor
📎DATA:
NBA player stats data obtained from web scraping as shown in a previous video:
Easy Web Scraping in Python using Pandas for Data Science
https://www.youtube.com/watch?v=ooj84UP3r6M
📎CODE: https://github.com/dataprofessor/code/blob/master/python/pandas_styling_dataframe.ipynb
⭕ Playlist:
Check out our other videos in the following playlists.
✅ Data Science 101: https://bit.ly/dataprofessor-ds101
✅ Data Science YouTuber Podcast: https://bit.ly/datascience-youtuber-podcast
✅ Data Science Virtual Internship: https://bit.ly/dataprofessor-internship
✅ Bioinformatics: http://bit.ly/dataprofessor-bioinformatics
✅ Data Science Toolbox: https://bit.ly/dataprofessor-datasciencetoolbox
✅ Streamlit (Web App in Python): https://bit.ly/dataprofessor-streamlit
✅ Shiny (Web App in R): https://bit.ly/dataprofessor-shiny
✅ Google Colab Tips and Tricks: https://bit.ly/dataprofessor-google-colab
✅ Pandas Tips and Tricks: https://bit.ly/dataprofessor-pandas
✅ Python Data Science Project: https://bit.ly/dataprofessor-python-ds
✅ R Data Science Project: https://bit.ly/dataprofessor-r-ds
⭕ Subscribe:
If you're new here, it would mean the world to me if you would consider subscribing to this channel.
✅ Subscribe: https://www.youtube.com/dataprofessor?sub_confirmation=1
⭕ Recommended Tools:
Kite is a FREE AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing. I've been using Kite and I love it!
✅ Check out Kite: https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=dataprofessor&utm_content=description-only
⭕ Recomm
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Data Professor · Data Professor · 58 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
▶
59
60
How a Biologist became a Data Scientist
Data Professor
WEKA Tutorial #1.1 - How to Build a Data Mining Model from Scratch
Data Professor
WEKA Tutorial #1.2 - How to Build a Data Mining Model from Scratch
Data Professor
WEKA Tutorial #1.3 - How to Build a Data Mining Model from Scratch
Data Professor
Computational Drug Discovery: Machine Learning for Making Sense of Big Data in Drug Discovery
Data Professor
Quotes #1 on Big Data and Data Science
Data Professor
Quotes #2 on Big Data and Data Science
Data Professor
Quotes #3 on Big Data and Data Science
Data Professor
Quotes #4 on Big Data and Data Science
Data Professor
Quotes #5 on Big Data and Data Science
Data Professor
Data Science 101: Starting a Data Science / Data Mining Project
Data Professor
Data Science 101: CRISP-DM - Data Mining / Data Science in 6 Steps
Data Professor
R Programming 101: How to Define Variables
Data Professor
R Programming 101: Read and Write CSV files
Data Professor
Data Science 101: Basic Command-Line for Data Science
Data Professor
Strategies for Learning Data Science in 2020 (Data Science 101)
Data Professor
Building your Data Science Portfolio with GitHub (Data Science 101)
Data Professor
R Programming 101: Setting up R programming environment (R, RStudio and RStudio.cloud)
Data Professor
Exploratory Data Analysis in R: Towards Data Understanding
Data Professor
Exploratory Data Analysis in R: Quick Dive into Data Visualization
Data Professor
Machine Learning in R: Building a Classification Model
Data Professor
Machine Learning in R: Repurpose Machine Learning Code for New Data
Data Professor
Data Science 101: Deploying your Machine Learning Model
Data Professor
Machine Learning in R: Deploy Machine Learning Model using RDS
Data Professor
Data Pre-processing in R: Handling Missing Data
Data Professor
Machine Learning in R: Speed up Model Building with Parallel Computing
Data Professor
Data Science 101: Overview of Machine Learning Model Building Process
Data Professor
Web Apps in R: Building your First Web Application in R | Shiny Tutorial Ep 1
Data Professor
Web Apps in R: Build Interactive Histogram Web Application in R | Shiny Tutorial Ep 2
Data Professor
Web Apps in R: Building Data-Driven Web Application in R | Shiny Tutorial Ep 3
Data Professor
Web Apps in R: Building the Machine Learning Web Application in R | Shiny Tutorial Ep 4
Data Professor
Web Apps in R: Build BMI Calculator web application in R for health monitoring | Shiny Tutorial Ep 5
Data Professor
Machine Learning in R: Building a Linear Regression Model
Data Professor
What programming language to learn for Data Science? R versus Python
Data Professor
How to Become a Data Scientist (Learning Path and Skill Sets Needed)
Data Professor
Using Python in R
Data Professor
Interpretable Machine Learning Models
Data Professor
Making Scatter Plots in R [Data Visualisation in R series]
Data Professor
Machine Learning in Python: Building a Classification Model
Data Professor
Compare Machine Learning Classifiers in Python
Data Professor
Hyperparameter Tuning of Machine Learning Model in Python
Data Professor
Practical Introduction to Google Colab for Data Science
Data Professor
File Handling in Google Colab for Data Science
Data Professor
Pandas for Data Science: Create and Combine DataFrames / Rename Columns
Data Professor
Machine Learning in Python: Building a Linear Regression Model
Data Professor
Machine Learning in Python: Principal Component Analysis (PCA) for Handling High-Dimensional Data
Data Professor
How to Plot an ROC Curve in Python | Machine Learning in Python
Data Professor
Installing conda on Google Colab for Data Science
Data Professor
Use native R on Google Colab for Data Science
Data Professor
How to Save and Download files from Google Colab
Data Professor
Easy Web Scraping in Python using Pandas for Data Science
Data Professor
Data Science for Computational Drug Discovery using Python (Part 1)
Data Professor
Pandas Profiling for Data Science (Quick and Easy Exploratory Data Analysis)
Data Professor
Exploratory Data Analysis in Python using pandas
Data Professor
Quick tour of PyCaret (a low-code machine learning library in Python)
Data Professor
How to Upload Files to Google Colab
Data Professor
How to Install and Use Pandas Profiling on Google Colab
Data Professor
How to Adjust the Style of Pandas DataFrame
Data Professor
How to use Bamboolib for Data Wrangling in Data Science
Data Professor
How to use Pandas Profiling on Kaggle
Data Professor
More on: ML Pipelines
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
You Are Not Behind. The World Is.
Medium · AI
Career choice with the advent of AI - pure Computer Science or learn software with a background of core engineering area
Dev.to AI
The AI Hype Cycle: Calm Before the Next Breakthrough?
Medium · Programming
AI won’t replace scientists. It will make the current model of science obsolete
Medium · Data Science
🎓
Tutor Explanation
DeepCamp AI