Bussiere
Bussiere

Reputation: 1144

Make a plot by occurence of a col by hour of a second col

I have this df :

and i would like to make a graph by half hour of how many row i have by half hour without including the day.

Just a graph with number of occurence by half hour not including the day.

3272    8711600410367   2019-03-11T20:23:45.415Z    d7ec8e9c5b5df11df8ec7ee130552944 home   2019-03-11T20:23:45.415Z    DISPLAY None
3273    8711600410367   2019-03-11T20:23:51.072Z    d7ec8e9c5b5df11df8ec7ee130552944 home   2019-03-11T20:23:51.072Z    DISPLAY None

Here is my try :

df["Created"] = pd.to_datetime(df["Created"])
df.groupby(df.Created.dt.hour).size().plot()

But it's not by half hour

I would like to show all half hour on my graph

enter image description here

Upvotes: 1

Views: 22

Answers (1)

Ruthger Righart
Ruthger Righart

Reputation: 4921

One way you could do this is split up coding for hours and half-hours, and then bring them together. To illustrate, I extended your data example a bit:

import pandas as pd
df = pd.DataFrame({'Created':['2019-03-11T20:23:45.415Z', '2019-03-11T20:23:51.072Z', '2019-03-11T20:33:03.072Z', '2019-03-11T21:10:10.072Z']})
df["Created"] = pd.to_datetime(df["Created"])

First create a 'Hours column':

df['Hours'] = df.Created.dt.hour

Then create a column that codes half hours. That is, if the minutes are greater than 30, count it as half hour.

df['HalfHours'] = [0.5 if x>30 else 0 for x in df.Created.dt.minute] 

Then bring them together again:

df['Hours_and_HalfHours'] = df['Hours']+df['HalfHours']

Finally, count the number of rows by groupby, and plot:

df.groupby(df['Hours_and_HalfHours']).size().plot()

Upvotes: 1

Related Questions