简体   繁体   中英

trying to find distinct values from the past week starting from today in a particular dataframe

I'm trying to find distinct values from a particular dataframe starting from today. I understand resample can do this but we are only able to set a particular arbitrary day, for example:

df.resample('W-TUE')

resamples the dataframe on a weekly basis starting from that particular day(in this case tuesday). Is there a way to do this by weekly frequency starting from today(dynamically ie, today could be any day between monday-sunday).

For example, if i have a dataframe which looks like this,

    sender_user_id    created
0   2                 2017-01-03 10:48:30.151437
1   2                 2017-01-03 09:48:30.151437
2   5                 2017-01-03 06:48:30.151437
3   14                2017-01-02 10:48:30.151437
4   12                2017-01-01 08:48:30.151437
5   11                2016-12-31 10:48:30.151437
6   17                2016-12-26 10:48:30.151437
7   21                2016-12-19 10:48:30.151437

and I want to get the unique user_ids for the past week starting from today, which could be any day(monday-sunday)

I think you can use datetime make today to "resample's particular day", like below:

In [1]: import datetime

In [2]: now = datetime.datetime.now()

In [3]: print now.strftime('%a')
Wed

In [4]: print now.strftime('W-%a')
W-Wed

Then you can use resample.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM