Crime Rate Over Time
Last updated
Last updated
Letโs try some simple exploratory plots at first and look at the general trend of crime cases in general. Can we find interesting trends and/or relations of crimes cases?
The result should look something like this:
How does your plot look like? A plot doesnโt have to be aesthetically pleasing, but has to convey your message without any further explanation. Another person should be able to get the quintessence of what you're trying to show by just seeing your plot. So let's add some more information.
After this step, you should expect the plot to look something like this:
Does this plot help you explain what you wanted to explain? The plot seems to be very "noisy" which makes it hard to find a general trend. Letโs smooth the data to make trends better visible. Here we will plot a (a.k.a. moving average)
The zoo
package has a function to calculate the rolling mean. You can find more about the rolling mean and more about the zoo
package .
: To analyze time-series data, resample()
is a powerful tool that adjusts the frequency of your data. Use it with datetime columns to group data into specific intervals (e.g., daily, monthly) and apply aggregation functions like sum()
, mean()
, or size()
. Unlike groupby()
, which requires explicit grouping columns, resample()
works directly with time-based data, making it ideal for tasks like counting events per day or calculating averages over weeks. Combine it with methods like rolling()
for smoothing trends and unstack()
for pivoting multi-level indexes to prepare your data for visualization. Check the pandas documentation for more examples!