Make Your Pandas Code Lightning Fast

Rob Mulla · Beginner ·📊 Data Analytics & Business Intelligence ·4y ago

Key Takeaways

Rob Mulla discusses a key trick to speed up slow pandas code by 2500x for improved performance

Full Transcript

pandas is an extremely popular python package for working and exploring data sets it's an essential tool for anyone interested in working with data in python and while many love and use it every day i'm surprised when i come across code that could be sped up in its efficiency just by making a few adjustments in this video i'm going to show you a trick that in our example makes the code run two thousand times faster and as you start working with larger and larger data sets this trick is going to become essential my name is rob and i make videos about coding in python and machine learning if you do enjoy these videos please consider subscribing liking the video and giving me a follow on twitch all right so let's look at speeding up some pandas code okay i hope you're excited to speed up some code here i am in a jupiter lab notebook and i am just going to start by making some imports so of course we want to import pandas as pd and import numpy as np and then we're going to create our data set we're only using our data set as an example so we're actually going to create some random data and we're going to create that as a data frame like this our data is going to be about fictitious people we'll give these people random ages using numpy's random let's give them a random integer between one and a hundred and let's make all of these the same size which will be ten thousand we'll also give the time in bed for these people ran random randint we're going to give the percentage of time sleeping now here we'll give some categorical features so we'll give the favorite food of the person let's give them pizza taco and ice cream and we'll give them some food that they hate so they hate food random choice of broccoli that's right we need to add the size candy corn and eggs and we can do this and see that we have our data set here with random data for the the people that we are simulating but we'll wrap this in a function so we can call it uh let's call it get data and add this size as a parameter and have it return this data frame so now if we call get data we get our random data set so we're going to make a fictitious problem up that we're trying to solve with pandas here the problem is we're taking the data that we have here and we're going to uh given some conditions we want to give each person a reward so reward calculation if they were in bed for more than five hours and they were sleeping for more than 50 percent of the time or 0.5 we give them their favorite food otherwise we give them their hate food and just to make it a little trickier we're also gonna say if they are over 90 years old give their favorite food regardless they've lived to be 90 years old so they deserve their favorite food so we can write this so we can write out this problem or this calculation in the form of a function that we will eventually apply to our data frame we can do it pretty easily let's call this reward calc and we'll give it a row or a person from our data frame writing our problem out as code we would say if the age of the person is greater than or equal to 90 will return their favorite food if the row time in bed is greater than 5 and the percent time sleeping is greater than point five then we return their favorite food and then otherwise we're going to return the hate food so this is the sort of thing you come across a lot when working with panda data sets you want to apply some sort of a function or logic across every row in the data set and we're going to do this three different ways from slowest to fastest now the first one we'll call level one which is looping this was always my first way of trying to solve this sort of problem is looping over each row of the data set and applying our known reward calculation now what we're going to find is it's not necessarily very fast but let's give it a try so we're going to first call our getdata function then we're going to iterate over each row using the it arose function so we're going to say for index row in data frame it arrows and then for each row we'll call our reward calc and we need to store this into our data frame at the index location that we are located in so we'll do loc dot index call this reward and store it you can see it's running now and there it's finished so we want to time this and see how long it actually takes and there's this nice magic tool that we can run on the top of our jupiter cell called time it and this will run it multiple times and give us the average amount of time it took to run it's nice to time things this way because then you get an idea over multiple times as opposed to just one try and it's finally done all right we see it ran for seven runs and each run was about 3.4 seconds that seems pretty slow let's make it faster so we're gonna go into level number two which is the apply function you may have heard of using the apply function it can be very useful in this case we are going to just take our data frame and we'll apply this count reward calculation function that we created and we're going to do it on the axis equals one so that we make sure it runs through each row and it's essentially going to do the same thing as our loop but more efficiently so let's store this as reward let's also get a new data frame every time and time it like we did above all right so it's done so it ran seven times and averaged 189 milliseconds we're already seeing a huge speed improvement by using apply instead of iterating over each row but there's more let's make it even faster let's go into level three and this is the key to speeding up most of your slow pandas code and that's using vectorized functions vectorized functions work very efficiently and they're when you apply functions like this across the whole data set instead of each row i'm going to show you how we do that here so what we would do is instead of using our pre-built function for each row if we actually apply each of these conditions to the whole data frame itself let's write each of them up so we're going to say the percent sleeping remember needs to be greater than 5 for it to be true and the time in bed needs to be greater than 5 or if the age is greater than 90. so this is a vectorized version of the same code that we wrote above but it's going to run a lot faster and what it will provide us back with is an array of true or false for if it meets this condition now what we're going to do is we're going to call our get data function again we're going to call reward is equal to the hate food except for when these conditions are met so we've already filled the reward in with all hate food but we're gonna locate when these conditions are met and when they are we'll give the reward of the favorite food and let's just split these lines up so it's a little bit easier to follow and let's go ahead and run time it on this wow that's a lot faster you can see it's 6.91 milliseconds and actually the a lot of the time that was taken to run this was just in the get data function so if i remove that get data function out of this 1.57 milliseconds let's just quickly plot what the difference is in time that it takes and i have some data saved off here from a previous run these are the results from a previous run where i had the milliseconds that looping applying and vectorize each took set our index to the type plot this as a bar plot and we can see here at the time it took to run each reward type the huge jump down was by changing from a loop to apply but then you can see that going from apply to using vectorized functions made it even more fast so the key is whenever you're writing functions on pandas try to use vectorize as much as you can it's not always the case that you're able to do it but when when possible use a vectorized function i hope you enjoyed this quick video showing you how you can speed up your pandas code always use vectorized functions as you can don't iterate or loop over it unless you need to thanks for watching and i'll see you in the next video

Original Description

Speed up slow pandas/python code by 2500x using this simple trick. Face it, your pandas code is slow. Learn how to speed it up! In this video Rob discusses a key trick to making your code faster! Pandas is an essential tool for any python programmer and data scientist. Using the pandas apply function, using vectorized functions, the speed difference can be significant. Write faster python code. Timeline 00:00 Intro 00:46 Creating our Data 02:39 The Problem 03:48 Coding Up the Problem 04:43 Level 1: Loop 06:29 Level 2: Apply 07:27 Level 3: Vectorized 09:31 Plot The Speed Comparison 10:23 Outro Follow me on twitch for live coding streams: https://www.twitch.tv/medallionstallion_ Intro to Pandas video: https://www.youtube.com/watch?v=_Eb0utIRdkw Exploritory Data Analysis Video: https://www.youtube.com/watch?v=xi0vhXFPegw * Youtube: https://youtube.com/@robmulla?sub_confirmation=1 * Discord: https://discord.gg/HZszek7DQc * Twitch: https://www.twitch.tv/medallionstallion_ * Twitter: https://twitter.com/Rob_Mulla * Kaggle: https://www.kaggle.com/robikscube #python #code #datascience #pandas
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Rob Mulla · Rob Mulla · 9 of 60

1 A Gentle Introduction to Pandas Data Analysis (on Kaggle)
A Gentle Introduction to Pandas Data Analysis (on Kaggle)
Rob Mulla
2 Exploratory Data Analysis with Pandas Python
Exploratory Data Analysis with Pandas Python
Rob Mulla
3 7 Python Data Visualization Libraries in 15 minutes
7 Python Data Visualization Libraries in 15 minutes
Rob Mulla
4 Kaggle competition starter notebook walkthrough
Kaggle competition starter notebook walkthrough
Rob Mulla
5 Kaggle Competitions: A Beginner's Guide to Winning
Kaggle Competitions: A Beginner's Guide to Winning
Rob Mulla
6 Jupyter Notebook Complete Beginner Guide - From Jupyter to Jupyterlab, Google Colab and Kaggle!
Jupyter Notebook Complete Beginner Guide - From Jupyter to Jupyterlab, Google Colab and Kaggle!
Rob Mulla
7 Audio Data Processing in Python
Audio Data Processing in Python
Rob Mulla
8 Complete Data Science Project!
Complete Data Science Project!
Rob Mulla
Make Your Pandas Code Lightning Fast
Make Your Pandas Code Lightning Fast
Rob Mulla
10 Image Processing with OpenCV and Python
Image Processing with OpenCV and Python
Rob Mulla
11 Speed Up Your Pandas Dataframes
Speed Up Your Pandas Dataframes
Rob Mulla
12 This INCREDIBLE trick will speed up your data processes.
This INCREDIBLE trick will speed up your data processes.
Rob Mulla
13 Complete Guide to Cross Validation
Complete Guide to Cross Validation
Rob Mulla
14 Easy Python Progress Bars with tqdm
Easy Python Progress Bars with tqdm
Rob Mulla
15 Economic Data Analysis Project with Python Pandas - Data scraping, cleaning and exploration!
Economic Data Analysis Project with Python Pandas - Data scraping, cleaning and exploration!
Rob Mulla
16 Python Sentiment Analysis Project with NLTK and 🤗 Transformers. Classify Amazon Reviews!!
Python Sentiment Analysis Project with NLTK and 🤗 Transformers. Classify Amazon Reviews!!
Rob Mulla
17 Get Started with Machine Learning and AI in 2023
Get Started with Machine Learning and AI in 2023
Rob Mulla
18 The Trick to Get Unlimited Datasets
The Trick to Get Unlimited Datasets
Rob Mulla
19 Video Data Processing with Python and OpenCV
Video Data Processing with Python and OpenCV
Rob Mulla
20 Object Detection in 10 minutes with YOLOv5 & Python!
Object Detection in 10 minutes with YOLOv5 & Python!
Rob Mulla
21 Pandas for Data Science #shorts
Pandas for Data Science #shorts
Rob Mulla
22 Object Detection in 60 Seconds using Python and YOLOv5 #shorts
Object Detection in 60 Seconds using Python and YOLOv5 #shorts
Rob Mulla
23 Machine Learning for Facial Recognition in Python in 60 Seconds #shorts
Machine Learning for Facial Recognition in Python in 60 Seconds #shorts
Rob Mulla
24 Time Series Forecasting with XGBoost - Use python and machine learning to predict energy consumption
Time Series Forecasting with XGBoost - Use python and machine learning to predict energy consumption
Rob Mulla
25 Detect Text in Images with Python - pytesseract vs. easyocr vs keras_ocr
Detect Text in Images with Python - pytesseract vs. easyocr vs keras_ocr
Rob Mulla
26 Solving an Impossible Riddle with Code
Solving an Impossible Riddle with Code
Rob Mulla
27 Do these Pandas Alternatives actually work?
Do these Pandas Alternatives actually work?
Rob Mulla
28 Time Series Forecasting with XGBoost - Advanced Methods
Time Series Forecasting with XGBoost - Advanced Methods
Rob Mulla
29 Data Science Uncut - Data Shootout Kaggle Competition (Aug 1 2022 Stream)
Data Science Uncut - Data Shootout Kaggle Competition (Aug 1 2022 Stream)
Rob Mulla
30 Kaggle Dataset Creation from Scratch- Data Science Uncut (Aug 10 2022)
Kaggle Dataset Creation from Scratch- Data Science Uncut (Aug 10 2022)
Rob Mulla
31 Chess Board Computer Vision AI - Data Science Uncut (Sep 7, 2022)
Chess Board Computer Vision AI - Data Science Uncut (Sep 7, 2022)
Rob Mulla
32 25 Nooby Pandas Coding Mistakes You Should NEVER make.
25 Nooby Pandas Coding Mistakes You Should NEVER make.
Rob Mulla
33 DEFCON Hacking AI CTF Solution on Kaggle - Data Science Uncut Sep 11, 2022
DEFCON Hacking AI CTF Solution on Kaggle - Data Science Uncut Sep 11, 2022
Rob Mulla
34 More Chessboard Computer Vision AI - Data Science Uncut - Sep 13
More Chessboard Computer Vision AI - Data Science Uncut - Sep 13
Rob Mulla
35 Medallion Data Science Live Stream
Medallion Data Science Live Stream
Rob Mulla
36 Community Kaggle Competition Overview - Corn Classification (
Community Kaggle Competition Overview - Corn Classification (
Rob Mulla
37 Deep Learning Image Classification - Corn Kernels - Data Science Uncut
Deep Learning Image Classification - Corn Kernels - Data Science Uncut
Rob Mulla
38 OpenAI Whisper Demo: Convert Speech to Text in Python
OpenAI Whisper Demo: Convert Speech to Text in Python
Rob Mulla
39 Yolov7 Custom Object Detection in Python Tutorial  - Chess Piece Detection
Yolov7 Custom Object Detection in Python Tutorial - Chess Piece Detection
Rob Mulla
40 Live Kaggle Coding - Enzyme Stability Prediction - Data Science Uncut Sep, 27 2022
Live Kaggle Coding - Enzyme Stability Prediction - Data Science Uncut Sep, 27 2022
Rob Mulla
41 Finding Chess Cheaters with Python! - Data Science Uncut Livestream
Finding Chess Cheaters with Python! - Data Science Uncut Livestream
Rob Mulla
42 Data Science Uncut - Kaggle Community Competition & Chess Data Analysis - Oct 4, 2022
Data Science Uncut - Kaggle Community Competition & Chess Data Analysis - Oct 4, 2022
Rob Mulla
43 Flight Delay Dataset Creation (Data Science Uncut)
Flight Delay Dataset Creation (Data Science Uncut)
Rob Mulla
44 5 Reasons to Kaggle #shorts
5 Reasons to Kaggle #shorts
Rob Mulla
45 ♟️ Data Science - Chess Data Analysis
♟️ Data Science - Chess Data Analysis
Rob Mulla
46 EXTREME PYTHON & DATA SCIENCE LIVE STREAM
EXTREME PYTHON & DATA SCIENCE LIVE STREAM
Rob Mulla
47 What is Clustering in ML?
What is Clustering in ML?
Rob Mulla
48 What is K-Nearest Neighbors?
What is K-Nearest Neighbors?
Rob Mulla
49 LIVE CODING: Flight Data Exploration with Pandas & Python
LIVE CODING: Flight Data Exploration with Pandas & Python
Rob Mulla
50 Kaggle Survey vs. Twitter Sentiment
Kaggle Survey vs. Twitter Sentiment
Rob Mulla
51 If Top Chess.com Players were STOCKS - Live Coding Data Anaylsis Stream
If Top Chess.com Players were STOCKS - Live Coding Data Anaylsis Stream
Rob Mulla
52 Data Visualization BATTLE!
Data Visualization BATTLE!
Rob Mulla
53 LIVE CODING: Stocks & Sentiment Analysis
LIVE CODING: Stocks & Sentiment Analysis
Rob Mulla
54 Progress Bar in Python with TQDM
Progress Bar in Python with TQDM
Rob Mulla
55 Flight Cancellation Data Analysis
Flight Cancellation Data Analysis
Rob Mulla
56 Synthetic Dataset Creation for Machine Learning - Blender and Python
Synthetic Dataset Creation for Machine Learning - Blender and Python
Rob Mulla
57 The Ultimate Coding Setup for Data Science
The Ultimate Coding Setup for Data Science
Rob Mulla
58 Dataset Creation SPEED RUN - Live Coding With Python & Pandas
Dataset Creation SPEED RUN - Live Coding With Python & Pandas
Rob Mulla
59 Data Wrangling with Python and Pandas LIVE
Data Wrangling with Python and Pandas LIVE
Rob Mulla
60 Forecasting with the FB Prophet Model
Forecasting with the FB Prophet Model
Rob Mulla

Related Reads

📰
I Built a Tool to Visualize DSA. Let’s Learn Together! (DSA View View 👀👀)
Learn to visualize Data Structures and Algorithms (DSA) with a custom tool built by a frontend engineer
Dev.to · nyaomaru
📰
Why More Organizations Are Embracing Conversational Analytics
Learn how conversational analytics is revolutionizing business intelligence by enabling organizations to make data-driven decisions through natural language interactions
Dev.to · Ravi Teja
📰
I Pre-Registered a Hypothesis. 600 API Calls Later, the Data Killed It.
Learn how to design and run an experiment to test a hypothesis using API calls and analyze the results to validate or invalidate the hypothesis
Dev.to · YuhaoLin2005
📰
Data Science Course in Ameerpet: Complete Guide for Beginners (2026)
Learn how to get started with a data science course in Ameerpet for a career switch or entry into the field
Medium · Machine Learning

Chapters (9)

Intro
0:46 Creating our Data
2:39 The Problem
3:48 Coding Up the Problem
4:43 Level 1: Loop
6:29 Level 2: Apply
7:27 Level 3: Vectorized
9:31 Plot The Speed Comparison
10:23 Outro
Up next
How to Scrape Facebook Ad Library Data + Analyse on n8n 🔥
DroidCrunch
Watch →