How Do I Aggregate Recorded Daily Incidents Into Monthly Counts
Question:
I have a large pandas dataframe of daily crime incidents. One of my columns (‘date’) is datatype datetime64[ns].
Take, for example, the dataframe below:
ID
Date
Crime
987AL4
1991-08-15
Robbery
987AL4
1991-08-16
Asasult
124576IL
1991-09-21
Breaking and Entering
…
…
…
4689CA
2016-08-17
Sexaul Assault
What I would like to do is create a new dataframe with monthly counts of incidents for each year and each ID.
So, ideally, the resulting dataframe might look something like:
ID
Month
Count
987AL4
1991-08
200,870
124576IL
1991-08
190,000
…
…
…
45678CA
2016-08
60,000
I’ve tried various groupby and count methods, but I’ve had no luck. I would greatly appreciate any help.
Answers:
You can convert your DatetimeIndex
as PeriodIndex
and use value_counts
:
out = (df.value_counts(['ID', df['Date'].dt.to_period('M').rename('Month')])
.rename('Count').reset_index())
print(out)
# Output
ID Month Count
0 987AL4 1991-08 2
1 124576IL 1991-09 1
2 4689CA 2016-08 1
I have a large pandas dataframe of daily crime incidents. One of my columns (‘date’) is datatype datetime64[ns].
Take, for example, the dataframe below:
ID | Date | Crime |
---|---|---|
987AL4 | 1991-08-15 | Robbery |
987AL4 | 1991-08-16 | Asasult |
124576IL | 1991-09-21 | Breaking and Entering |
… | … | … |
4689CA | 2016-08-17 | Sexaul Assault |
What I would like to do is create a new dataframe with monthly counts of incidents for each year and each ID.
So, ideally, the resulting dataframe might look something like:
ID | Month | Count |
---|---|---|
987AL4 | 1991-08 | 200,870 |
124576IL | 1991-08 | 190,000 |
… | … | … |
45678CA | 2016-08 | 60,000 |
I’ve tried various groupby and count methods, but I’ve had no luck. I would greatly appreciate any help.
You can convert your DatetimeIndex
as PeriodIndex
and use value_counts
:
out = (df.value_counts(['ID', df['Date'].dt.to_period('M').rename('Month')])
.rename('Count').reset_index())
print(out)
# Output
ID Month Count
0 987AL4 1991-08 2
1 124576IL 1991-09 1
2 4689CA 2016-08 1