1. Are drug-related stops on the rise?
In the last section, we created a plot to help us see how a single variable changed over time. In this section, we'll use subplots to help us examine the relationship between two variables over time.
2. Resampling the price
Let's return to our DataFrame of Apple stock prices. In the last section, we calculated the mean price for each month using a groupby() operation.
There's actually an alternative method, known as resampling, that we can use to accomplish the same task. As you might remember from previous courses, resampling is when you change the frequency of your time series observations. In this case, we'll resample the price column by month, represented by the capital letter M, and then chain the mean() method onto the end. The output is identical to the groupby() operation, except that the index consists of the last day of each month, rather than just the integers 1, 2, and 3.
3. Resampling the volume
As a reminder, the apple DataFrame also has a volume column that displays the number of Apple shares traded that day. Let's resample that column, to calculate the mean daily volume for each month.
One thing worth noticing is that the output has the same index as the last resampling operation, since both price and volume were resampled at the same frequency.
4. Concatenating price and volume
Now that we've resampled both the price and volume data, we're going to combine the results into a single DataFrame so that we can study the relationship between price and volume.
First, we'll save the two resampled Series as separate objects. One is called monthly_price and the other is called monthly_volume.
Then, we'll combine these two objects using the concat() function, which concatenates pandas objects along a specified axis. In this case, we want them to be combined along the columns axis, meaning that we want them side-by-side. Notice that they aligned along their shared index.
We'll save this new DataFrame as an object called monthly.
5. Plotting price and volume (1)
To visualize the relationship between price and volume, we simply call the plot() method on the monthly DataFrame. pandas outputs a single line plot in which each line represents one of the two columns.
Because it's a single plot, the two lines are sharing both the x and y axes. It makes sense for the x-axis to be shared, since we want to compare price and volume by month. But sharing the y-axis is problematic, because price and volume are on such different scales: price is in the hundreds, and volume is in the tens of millions, represented by the 1e7 notation at the top of the y-axis. Because of the different scales, we can't actually see the price trend.
6. Plotting price and volume (2)
The solution to this problem is to set the subplots parameter to True, which results in two separate plots with independent y axes. Now we can clearly see the monthly trends for both price and volume. They appear to have an inverse relationship, though we wouldn't actually draw that conclusion without having a lot more data.
7. Let's practice!
Now it's time for you to practice resampling, concatenation, and subplots while you examine drug-related traffic stops in our dataset.