Hi Reader,
Last week, I released a 3-hour video, My top 50 scikit-learn tips.
I also finished Chapter 10 of my next ML course, which I'll tell you about once all 20 chapters are done 😅
Anyway, let’s get to today’s tip!
For fun, I’ve been building an interactive dashboard using pandas, Plotly Express, and Shiny for Python. (Check out a screenshot here.)
The goal is to help me analyze sales of my online courses. And since I’m working with sales data, I’m reminded of how much I love the pandas resample function!
Let’s see an example of how to resample 😉
Pretend you have a DataFrame of sales data that looks like this:
You might ask: What are my total sales for each product?
In that case, you would use groupby:
You can read that code as: For each Product, this is the sum of the Sale column.
Another similar question you might ask is: What are my total sales for each day?
Instead of groupby, you would use resample, which I think of as “groupby for time series data”:
You can read that code as: For each day, this is the sum of the Sale column.
(Notice that it inserted 2023-03-31 with a value of 0, since there were no sales on that day.)
By changing the 'D' to an 'M', you can resample by month instead:
'D' and 'M' are known as the “offset alias”, and there are many other offset aliases you can use.
Finally, let’s say that the index is not a datetime column:
In that case, you need to use the 'on' parameter to specify the datetime column:
If you work with time series data, I bet you’ll find a use for resample!
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. What’s the worst volume control interface? (my favorite is #12)
Did someone awesome forward you this email? Sign up here to receive data science tips every week!
Join 25,000+ aspiring Data Scientists and receive Python & Data Science tips every Tuesday!
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...