Silentbob
Silentbob

Reputation: 3065

Pandas group times into hour periods and count

I have a dataframe where I have created a column called newTime which is based on the time from a dateTime column called InvoiceDate.

df['newTime'] = [d.time() for d in df['InvoiceDate']]

In this column is a list of times using the format HH:mm:ss

How do I group these times into hour periods for a full 24 hours and then get a count for each period?

So the dataframe column looks like:

Dataframe

And the expected output looks like the following (or similar):

expected output

Upvotes: 1

Views: 111

Answers (1)

jezrael
jezrael

Reputation: 862511

I believe you need aggregate size by hours:

df1 = df.groupby(df['InvoiceDate'].dt.hour).size().reset_index(name='count')

Upvotes: 1

Related Questions