Hi Reader!
Before we get to today's tip, I have three big launch announcements:
1. My new course, Become a Regex Superhero, launches this Friday! Watch out for more info and a huge launch discount.
2. I recently launched location-based pricing. I'm offering a discount of up to 85% to residents of 160+ countries.
3. My pal Ben Collins just launched a new course, Beginner Apps Script, which lets you extend the functionality of Google Sheets (and other Google Apps). Ben is the best Sheets teacher I know and is offering 50% off during the launch! (FYI, this is an affiliate link, which means that I may earn a commission if you sign up using my link.)
Are you watching March Madness? If so, hit reply and let me know how your bracket is doing π
For those who don't know, March Madness is a US college basketball tournament. One term that you'll hear a lot during games is scoring runs.
For example, a team that's on a "12-point scoring run" has scored 12 points without the other team scoring any points.
So I was wondering: How could we calculate scoring runs using pandas? πΌ
Let's find out!
Let's pretend this was our scoring data. There's one row for each time a team scored points:
In this case, the largest scoring run was when A scored 9 points in a row.
Now we need to figure out when each scoring run starts!
First, we use the shift() method to shift all of the teams down a row, and store those in a column called previous_team:
Then, we check if team is not equal to previous_team, and store the boolean result in a column called start_of_run:
Do you see how that works?
By checking whether a given team value is equal to the value in the previous row, we now know when each scoring run starts!
Finally, we use the cumsum() method to assign a run_id to each scoring run:
Wait, what just happened?
Any time you do math on a boolean column, True gets treated as 1 and False gets treated as 0. Thus by taking the cumulative sum of the start_of_run column, the run_id increments every time it reaches a True value. (Neat, right?)
Shout out to Josh Devlin's excellent blog post, Calculating Streaks in Pandas, for teaching me this exact approach!
Now that each run has been assigned an id, we use a groupby() to show the number of points scored by each team during each run:
That's it! Here's the code from today's tip, in case you want to play around with it.
How else could we analyze this data using pandas? π€
Have an idea? Hit reply and let me know! π‘
If you enjoyed this week's tip, please forward it to a friend! Takes only a few seconds, and it really helps me out π
See you next Tuesday!
- Kevin
P.S. Six people predicted the Final Four correctlyβ
Did someone awesome forward you this email? Sign up here to receive data science tips every week!
Join 25,000+ intelligent readers and receive AI tips every Tuesday!
Hi Reader, This week, I've got a short tip about AI agents, followed by some Data School news... π Tip #56: What are AI agents? Google is calling 2025 "the agentic era," DeepLearning.AI says "the agentic era is upon us," and NVIDIA's founder says "one of the most important things happening in the world of enterprise is agentic AI." Clearly AI agents are a big deal, but what exactly are they? Simply put, an AI agent is an application that uses a Large Language Model (LLM) to control its...
Hi Reader, Last week, I launched a brand new course: Build an AI chatbot with Python. 120+ people enrolled, and a few have already completed the course! π Want to join us for $9? π Tip #55: Should you still learn to code in 2025? Youβve probably heard that Large Language Models (LLMs) are excellent at writing code: They are competitive with the best human coders. They can create a full web application from a single prompt. LLM-powered tools like Cursor and Copilot can autocomplete or even...
Hi Reader, The Python 14-Day Challenge starts tomorrow! Hope to see you there π€ π Tuesday Tip: My top 5 sources for keeping up with AI I'll state the obvious: AI is moving incredibly FAST π¨ Here are the best sources I follow to keep up with the most important developments in Artificial Intelligence: The Neuron (daily newsletter) My top recommendation for a general audience. Itβs fun, informative, and well-written. It includes links to the latest AI news and tools, but the real goldmine is...