Reputation: 33
I have this data in mysql and i have read it into pandas using read_sql. Data:
count InTime Day
0 1 19:10:14 2021-02-28
1 2 19:10:26 2021-02-28
2 3 19:10:47 2021-02-28
3 4 19:10:47 2021-02-28
4 1 20:06:53 2021-02-28
5 2 20:07:03 2021-02-28
6 3 20:07:23 2021-02-28
7 4 20:07:23 2021-02-28
8 1 10:32:57 2021-03-01
9 2 10:33:08 2021-03-01
10 3 10:33:28 2021-03-01
11 4 10:33:28 2021-03-01
I have converted the columns to the required datetime format. Now i would like to find the count on 28th between 19:00:00 and 20:00:00. How do i do this ?
Upvotes: 2
Views: 48
Reputation: 862406
You can create datetimes anf filter by Series.between
in boolean indexing
:
df['datetime'] = pd.to_datetime(df['Day'] + ' ' + df['InTime'])
df1 = df[df['datetime'].between('2021-02-28 19:00:00', '2021-02-28 20:00:00')]
print (df1)
count InTime Day datetime
0 1 19:10:14 2021-02-28 2021-02-28 19:10:14
1 2 19:10:26 2021-02-28 2021-02-28 19:10:26
2 3 19:10:47 2021-02-28 2021-02-28 19:10:47
3 4 19:10:47 2021-02-28 2021-02-28 19:10:47
If need filter only count
column use DataFrame.loc
:
s = df.loc[df['datetime'].between('2021-02-28 19:00:00', '2021-02-28 20:00:00'), 'count']
print (s)
0 1
1 2
2 3
3 4
Name: count, dtype: int64
Upvotes: 1