singmotor
singmotor

Reputation: 4180

Graphing number of rows over time in pandas

I have a data frame with rows of datetime elements (or perhaps they're text, I'm reading them in from a csv)Ex:. 2017-07-14 09:10:40 2017-07-14 09:10:24 2017-07-14 09:10:22 2017-07-14 09:09:49 2017-07-14 09:09:48 2017-07-14 09:09:48 2017-07-14 09:09:26 2017-07-14 09:09:04 2017-07-14 09:08:35 2017-07-14 09:08:17 2017-07-14 09:08:07

I'd like to graph how many rows there are per date, or per hour. (dates on x axis and number of rows on y axis).

How can I do that? I recognize that I'll need to do a count, but I don't know what to do with the number once I have it for each date. I guess I'll need to map it to a new df or something?

Thanks!

Upvotes: 4

Views: 4444

Answers (1)

jezrael
jezrael

Reputation: 863116

I think you need groupby by dt.date or dt.hour and aggregate size, last plot:

df.groupby(df['Date'].dt.date).size().plot()

Or:

#change axis name to Hours by rename
df = df.groupby(df['Date'].rename('Hours').dt.hour).size().plot()

Sample:

rng = pd.date_range('2017-04-03', periods=15, freq='3.5H')
df = pd.DataFrame({'Date': rng})  
print (df)
                  Date
0  2017-04-03 00:00:00
1  2017-04-03 03:30:00
2  2017-04-03 07:00:00
3  2017-04-03 10:30:00
4  2017-04-03 14:00:00
5  2017-04-03 17:30:00
6  2017-04-03 21:00:00
7  2017-04-04 00:30:00
8  2017-04-04 04:00:00
9  2017-04-04 07:30:00
10 2017-04-04 11:00:00
11 2017-04-04 14:30:00
12 2017-04-04 18:00:00
13 2017-04-04 21:30:00
14 2017-04-05 01:00:00

print (df.groupby(df['Date'].rename('Hours').dt.hour).size())
Hours
0     2
1     1
3     1
4     1
7     2
10    1
11    1
14    2
17    1
18    1
21    2
dtype: int64

df.groupby(df['Date'].rename('Hours').dt.hour).size().plot()

graph

df.groupby(df['Date'].rename('Hours').dt.hour).size().plot.bar()

graph1

Upvotes: 4

Related Questions