3

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!

1 Answer 1

3

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 )]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.