Let it be the following Python Panda DataFrame:
date | week_day | flag |
---|---|---|
2022-04-29 19:03:21 | Friday | False |
2022-05-23 21:30:04 | Monday | False |
2022-05-29 00:50:45 | Sunday | True |
2022-06-10 20:07:21 | Friday | False |
2022-06-17 17:23:22 | Friday | False |
2022-06-18 21:30:08 | Saturday | True |
I want the flag to be True on Fridays from 19:00:00. That is to say, to obtain the following resulting table.
date | week_day | flag |
---|---|---|
2022-04-29 19:03:21 | Friday | True |
2022-05-23 21:30:04 | Monday | False |
2022-05-29 00:50:45 | Sunday | True |
2022-06-10 20:07:21 | Friday | True |
2022-06-17 17:23:22 | Friday | False |
2022-06-18 21:30:08 | Saturday | True |
CodePudding user response:
here is one way to accomplish it
filter the rows where day is Friday and then using datetime, split out the hour and compare if its 19 or larger and accordingly set the flag as true, else keep it as it is
np.where allows to conditionally update the flag
import datetime
import numpy as np
import pandas as pd
df['flag'] = np.where((df['week_day'] == 'Friday') & (df['date'].astype('datetime64').dt.strftime('%H') >= '19'),
True,
df['flag'])
Alternately, use dt.hour instead of the strftime, as Quang Hoang suggested in comment.
df['flag'] = np.where((df['week_day'] == 'Friday') & (df['date'].astype('datetime64').dt.hour >= 19),
True,
df['flag'])
df
date week_day flag
0 2022-04-29 19:03:21 Friday True
1 2022-05-23 21:30:04 Monday False
2 2022-05-29 00:50:45 Sunday True
3 2022-06-10 20:07:21 Friday True
4 2022-06-17 17:23:22 Friday False
5 2022-06-18 21:30:08 Saturday True