Data Science Jumpstart with 10 Projects Transcripts
Chapter: Project 6: Working with Time Series - Air Quality over Time
Lecture: Creating 7 Day Rolling Averages in Pandas with rolling

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's look at doing rolling averages. This is something you'll probably want to be able to do as well.
0:05 Here's a plot. This is going from April 2004 to May 2005. Now it's a little bit hard to see, this looks kind of like a caterpillar.
0:14 Let me show you a cool thing that we can do. One thing that we could do is we could say let's resample this to the day level.
0:19 And here's the resampled day level version of that. In addition to doing that, we can say, okay, let's do a rolling version of that.
0:31 So this is what I had before. And now what I'm going to do is I'm going to say I want to roll that.
0:40 So when I do rolling, it's lazy, it doesn't do anything, but what it's going to do is give me a window.
0:44 And this is a window that's going to move or slide along my data, and then I can apply an aggregation as it slides.
0:51 So it doesn't do anything until I apply the aggregation. Let's apply the aggregation.
0:56 So the aggregation is mean here, and you can see that the first few entries don't have any values because I need seven of them.
1:03 But basically this is taking the mean of the previous six values with the current value.
1:10 Let's plot that and see what it looks like. And it looks like this. So this is the rolling seven day average of this data up here.
1:20 This lets us spot trends really easy. One of the places you see this is if you have data that's kind of jagged.
1:26 It's super common for COVID data. We saw this a lot with visualizations where you would have daily data,
1:31 and then you would have visualization people would come in and put a rolling seven day average on top of that to see trends
1:39 because sometimes they wouldn't measure things on the weekend or whatnot. And so the rolling version would let you see a trend really easily.


Talk Python's Mastodon Michael Kennedy's Mastodon