Reputation: 35
I have a DataFrame which has a DateTime index as such
What I would like to do would be to find how many rows/instances occur by year. E.g. how many instances occur in the year 2013, 2014 ... 2020.
I would like to return this as a series or dataframe which I can then use to plot to show how many instances there were per year e.g.
2013 20
2014 14
.
.
.
2020 25
Is there a way to use the groupby or another function on the datetime index to return the counts of how many rows there are per year?
Upvotes: 0
Views: 1184
Reputation: 4761
You can use pandas.DataFrame.groupby
with a callable:
df.groupby(lambda x: x.year).size()
When you use groupby
this way, the callable works directly with the indices.
This is equivalent to:
def func(date):
return date.year
df.groupby(func).size()
Since you're working with pandas.Timestamp
you can easily access to the year
attribute.
Upvotes: 1