Tuesday Tip #9: Calculate scoring runs in basketball 🏀


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.)


👉 Tip #9: Calculate basketball scoring runs

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!


Example scoring data

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.


Identify each scoring run

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!


List all scoring runs

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!

Learn Data Science from Data School 📊

Join 25,000+ aspiring Data Scientists and receive Python & Data Science tips every Tuesday!

Read more from Learn Data Science from Data School 📊

Hi Reader, Next week, I’ll be offering a Black Friday sale on ALL of my courses. I’ll send you the details this Thursday! 🚨 👉 Tip #50: What is a "method" in Python? In Python, a method is a function that can be used on an object because of the object's type. For example, if you create a Python list, the "append" method can be used on that list. All lists have an "append" method simply because they are lists: If you create a Python string, the "upper" method can be used on that string simply...

Hi Reader, I appreciate everyone who has emailed to check on me and my family post-Helene! It has been more than 6 weeks since the hurricane, and most homes in Asheville (mine included) still don't have clean, running water. We're hopeful that water service will return within the next month. In the meantime, we're grateful for all of the aid agencies providing free bottled water, free meals, places to shower, and so much more. ❤️ Thanks for allowing me to share a bit of my personal life with...

Hi Reader, Regardless of whether you enrolled, thanks for sticking with me through the launch of my new course! 🚀 I've already started exploring topics for the next course... 😄 🔗 Link of the week git cheat sheet (PDF) A well-organized and highly readable cheat sheet from Julia Evans, the brilliant mind behind Wizard Zines! 👉 Tip #48: Three ways to set your environment variables in Python I was playing around with Mistral LLM this weekend (via LangChain in Python), and I needed to set an...