How to Adjust the Style of Pandas DataFrame

Data Professor · Beginner ·📰 AI News & Updates ·6y ago
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 How a Biologist became a Data Scientist
How a Biologist became a Data Scientist
Data Professor
2 WEKA Tutorial #1.1 - How to Build a Data Mining Model from Scratch
WEKA Tutorial #1.1 - How to Build a Data Mining Model from Scratch
Data Professor
3 WEKA Tutorial #1.2 - How to Build a Data Mining Model from Scratch
WEKA Tutorial #1.2 - How to Build a Data Mining Model from Scratch
Data Professor
4 WEKA Tutorial #1.3 - How to Build a Data Mining Model from Scratch
WEKA Tutorial #1.3 - How to Build a Data Mining Model from Scratch
Data Professor
5 Computational Drug Discovery: Machine Learning for Making Sense of Big Data in Drug Discovery
Computational Drug Discovery: Machine Learning for Making Sense of Big Data in Drug Discovery
Data Professor
6 Quotes #1 on Big Data and Data Science
Quotes #1 on Big Data and Data Science
Data Professor
7 Quotes #2 on Big Data and Data Science
Quotes #2 on Big Data and Data Science
Data Professor
8 Quotes #3 on Big Data and Data Science
Quotes #3 on Big Data and Data Science
Data Professor
9 Quotes #4 on Big Data and Data Science
Quotes #4 on Big Data and Data Science
Data Professor
10 Quotes #5 on Big Data and Data Science
Quotes #5 on Big Data and Data Science
Data Professor
11 Data Science 101: Starting a Data Science / Data Mining Project
Data Science 101: Starting a Data Science / Data Mining Project
Data Professor
12 Data Science 101: CRISP-DM - Data Mining / Data Science in 6 Steps
Data Science 101: CRISP-DM - Data Mining / Data Science in 6 Steps
Data Professor
13 R Programming 101: How to Define Variables
R Programming 101: How to Define Variables
Data Professor
14 R Programming 101: Read and Write CSV files
R Programming 101: Read and Write CSV files
Data Professor
15 Data Science 101: Basic Command-Line for Data Science
Data Science 101: Basic Command-Line for Data Science
Data Professor
16 Strategies for Learning Data Science in 2020 (Data Science 101)
Strategies for Learning Data Science in 2020 (Data Science 101)
Data Professor
17 Building your Data Science Portfolio with GitHub (Data Science 101)
Building your Data Science Portfolio with GitHub (Data Science 101)
Data Professor
18 R Programming 101: Setting up R programming environment (R, RStudio and RStudio.cloud)
R Programming 101: Setting up R programming environment (R, RStudio and RStudio.cloud)
Data Professor
19 Exploratory Data Analysis in R: Towards Data Understanding
Exploratory Data Analysis in R: Towards Data Understanding
Data Professor
20 Exploratory Data Analysis in R: Quick Dive into Data Visualization
Exploratory Data Analysis in R: Quick Dive into Data Visualization
Data Professor
21 Machine Learning in R: Building a Classification Model
Machine Learning in R: Building a Classification Model
Data Professor
22 Machine Learning in R: Repurpose Machine Learning Code for New Data
Machine Learning in R: Repurpose Machine Learning Code for New Data
Data Professor
23 Data Science 101: Deploying your Machine Learning Model
Data Science 101: Deploying your Machine Learning Model
Data Professor
24 Machine Learning in R: Deploy Machine Learning Model using RDS
Machine Learning in R: Deploy Machine Learning Model using RDS
Data Professor
25 Data Pre-processing in R: Handling Missing Data
Data Pre-processing in R: Handling Missing Data
Data Professor
26 Machine Learning in R: Speed up Model Building with Parallel Computing
Machine Learning in R: Speed up Model Building with Parallel Computing
Data Professor
27 Data Science 101: Overview of Machine Learning Model Building Process
Data Science 101: Overview of Machine Learning Model Building Process
Data Professor
28 Web Apps in R: Building your First Web Application in R | Shiny Tutorial Ep 1
Web Apps in R: Building your First Web Application in R | Shiny Tutorial Ep 1
Data Professor
29 Web Apps in R: Build Interactive Histogram Web Application in R | Shiny Tutorial Ep 2
Web Apps in R: Build Interactive Histogram Web Application in R | Shiny Tutorial Ep 2
Data Professor
30 Web Apps in R: Building Data-Driven Web Application in R | Shiny Tutorial Ep 3
Web Apps in R: Building Data-Driven Web Application in R | Shiny Tutorial Ep 3
Data Professor
31 Web Apps in R: Building the Machine Learning Web Application in R | Shiny Tutorial Ep 4
Web Apps in R: Building the Machine Learning Web Application in R | Shiny Tutorial Ep 4
Data Professor
32 Web Apps in R: Build BMI Calculator web application in R for health monitoring | Shiny Tutorial Ep 5
Web Apps in R: Build BMI Calculator web application in R for health monitoring | Shiny Tutorial Ep 5
Data Professor
33 Machine Learning in R: Building a Linear Regression Model
Machine Learning in R: Building a Linear Regression Model
Data Professor
34 What programming language to learn for Data Science? R versus Python
What programming language to learn for Data Science? R versus Python
Data Professor
35 How to Become a Data Scientist (Learning Path and Skill Sets Needed)
How to Become a Data Scientist (Learning Path and Skill Sets Needed)
Data Professor
36 Using Python in R
Using Python in R
Data Professor
37 Interpretable Machine Learning Models
Interpretable Machine Learning Models
Data Professor
38 Making Scatter Plots in R [Data Visualisation in R series]
Making Scatter Plots in R [Data Visualisation in R series]
Data Professor
39 Machine Learning in Python: Building a Classification Model
Machine Learning in Python: Building a Classification Model
Data Professor
40 Compare Machine Learning Classifiers in Python
Compare Machine Learning Classifiers in Python
Data Professor
41 Hyperparameter Tuning of Machine Learning Model in Python
Hyperparameter Tuning of Machine Learning Model in Python
Data Professor
42 Practical Introduction to Google Colab for Data Science
Practical Introduction to Google Colab for Data Science
Data Professor
43 File Handling in Google Colab for Data Science
File Handling in Google Colab for Data Science
Data Professor
44 Pandas for Data Science: Create and Combine DataFrames / Rename Columns
Pandas for Data Science: Create and Combine DataFrames / Rename Columns
Data Professor
45 Machine Learning in Python: Building a Linear Regression Model
Machine Learning in Python: Building a Linear Regression Model
Data Professor
46 Machine Learning in Python: Principal Component Analysis (PCA) for Handling High-Dimensional Data
Machine Learning in Python: Principal Component Analysis (PCA) for Handling High-Dimensional Data
Data Professor
47 How to Plot an ROC Curve in Python | Machine Learning in Python
How to Plot an ROC Curve in Python | Machine Learning in Python
Data Professor
48 Installing conda on Google Colab for Data Science
Installing conda on Google Colab for Data Science
Data Professor
49 Use native R on Google Colab for Data Science
Use native R on Google Colab for Data Science
Data Professor
50 How to Save and Download files from Google Colab
How to Save and Download files from Google Colab
Data Professor
51 Easy Web Scraping in Python using Pandas for Data Science
Easy Web Scraping in Python using Pandas for Data Science
Data Professor
52 Data Science for Computational Drug Discovery using Python (Part 1)
Data Science for Computational Drug Discovery using Python (Part 1)
Data Professor
53 Pandas Profiling for Data Science (Quick and Easy Exploratory Data Analysis)
Pandas Profiling for Data Science (Quick and Easy Exploratory Data Analysis)
Data Professor
54 Exploratory Data Analysis in Python using pandas
Exploratory Data Analysis in Python using pandas
Data Professor
55 Quick tour of PyCaret (a low-code machine learning library in Python)
Quick tour of PyCaret (a low-code machine learning library in Python)
Data Professor
56 How to Upload Files to Google Colab
How to Upload Files to Google Colab
Data Professor
57 How to Install and Use Pandas Profiling on Google Colab
How to Install and Use Pandas Profiling on Google Colab
Data Professor
How to Adjust the Style of Pandas DataFrame
How to Adjust the Style of Pandas DataFrame
Data Professor
59 How to use Bamboolib for Data Wrangling in Data Science
How to use Bamboolib for Data Wrangling in Data Science
Data Professor
60 How to use Pandas Profiling on Kaggle
How to use Pandas Profiling on Kaggle
Data Professor

This video teaches how to customize the appearance of Pandas DataFrames using CSS styling, which is essential for data visualization and presentation. By following the steps, viewers can learn how to change header colors, row colors, and hover highlight colors.

Key Takeaways
  1. Import necessary libraries (numpy and pandas)
  2. Download NBA player stats data from github
  3. Read in pandas data frame and display selected columns
  4. Define f equals to DF and display only selected columns
  5. Use set_table_styles function to apply CSS styling to data frame
  6. Style Pandas DataFrame using color coding
  7. Display index numbers and values with specific colors
💡 The set_table_styles function in pandas can be used to apply CSS styling to data frames, allowing for customization of the table appearance.

Related AI Lessons

You Are Not Behind. The World Is.
You're not behind, the world is still adapting to AI, and it's okay to take your time to learn and grow
Medium · AI
Career choice with the advent of AI - pure Computer Science or learn software with a background of core engineering area
Learn how to choose between a Computer Science and Engineering career path or combining programming with a core engineering background in the age of AI
Dev.to AI
The AI Hype Cycle: Calm Before the Next Breakthrough?
Understand the AI hype cycle to anticipate the next breakthrough and make informed decisions
Medium · Programming
AI won’t replace scientists. It will make the current model of science obsolete
AI is not replacing scientists, but rather making the current model of science obsolete, enabling new forms of discovery and collaboration
Medium · Data Science
Up next
Motorist saved by human chain | 9 News Australia
9 News Australia
Watch →