Pandas sample dataframe with Date time column and random values with incremental numbers
To generate the DataFrame df, run the code given at the end of this page. As the DataFrame is generated based on todays date and randomly selected values from different lists , so the result will change and every time a different DataFrame is generated.
Here is the sample output.
today=pd.to_datetime('now')
#today=pd.to_datetime('2022-04-12') # change this date YYYY-MM-DD
new_day=today-datetime.timedelta(7)
print(df[(df['dt']>new_day)])
Output
Log_ID dt dept activity eqpt
358 1358 2022-04-19 Dept2 Inspection Fan
359 1359 2022-04-20 Dept4 Checking Battery
360 1360 2022-04-21 Dept2 Painting AC
361 1361 2022-04-22 Dept1 Cleaning AC
362 1362 2022-04-23 Dept4 Cleaning Server
363 1363 2022-04-24 Dept4 Cleaning UPS
364 1364 2022-04-25 Dept4 Painting AC
List all records having done ( dt) on Wednesday ( Weekday =2 )
Monday is weekday = 0 , Tuesday =1, Sunday is Weekday = 6
print(df[df['dt'].dt.weekday==2])
To check the weekday we can add one more column saying weekday. We can add our format by using strftime