I have a pandas dataframe which looks like:
df = pd.DataFrame(data={'id':[1234, 1234, 1234, 1234, 1234], 'year':['2017', '2017', '2018', '2018', '2018'], 'count_to_today':[1, 2, 3, 3, 4]})
df
id year count_to_today
0 1234 2017 1
1 1234 2017 2
2 1234 2018 3
3 1234 2018 3
4 1234 2018 4
And I need to count how many times count_to_today
happens in each year per id
. i.e. I have a running count since the beginning of time, and I want to count the number of times it increments per year.
count_in_year
id year
1234 2017 2
2018 2
I'm a bit confused about how to do this. I know I need to groupby id
and year
but I can't figure out how to get .count()
or .value_counts()
to give me the counts per year.