Suppose I have a pandas data frame with multiple columns- something like this
targetName ... atRiskCommonPurchased
0 Twitter, Inc. ... NaN
1 Forbes Media ... NaN
2 Adobe ... NaN
3 Virgin Airlines ... NaN
4 H&M ... NaN
[5 rows x 51 columns]
And I have also have a column in the same data frame which is of dates:
df['dealAnnouncementDate'].dtype
dtype('O')
print(df['dealAnnouncementDate'])
0 2021-08-30
1 2021-08-26
2 2021-08-25
3 2021-08-23
4 2021-08-18
using df.between() I can easily filter the dataframe using the date column and get the result. something like: df[df['dealAnnouncementDate'].between('7/27/2021', '8/27/2021')]
But, I would be runnning the python script on every 7th day(take for instance Monday), How do I ensure that using dealAnnouncementDate column I get filtered results for 7 days? Please help me to understand how can it be done! Thanks!
If need filter between some dates, e.g. now and next 7 days use:
df['dealAnnouncementDate'] = pd.to_datetime(df['dealAnnouncementDate'])
now = pd.to_datetime('now').normalize()
print (now)
2021-08-31 00:00:00
df[df['dealAnnouncementDate'].between(now, now + pd.Timedelta('7days'))]
For previous 7 days:
df[df['dealAnnouncementDate'].between(now - pd.Timedelta('7days'), now )]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With