简体   繁体   中英

Counting the number of rows by year with a DateTime Index

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. Eg 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 eg

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM