看看洛杉矶有没有唱片

2024-05-20 01:06:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要计算客户是否经常出现。如果在过去一周内有活动,则定义经常性客户机

我有一张这样的桌子:

DATE       | Document | CUSTOMER
2018-08-14 | 12038120 | A

到目前为止,我只能得到一个计数的活动,每个客户每个日期。但是从今天到过去的7天我都不知道怎么数数了

到目前为止,我有:

df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')

df= df.set_index('Date')
df= df.sort_index()

df= df.groupby(['Customer', 'Date'])['Document'].count()

谢谢


Tags: todfdate客户机index客户定义customer
1条回答
网友
1楼 · 发布于 2024-05-20 01:06:25

使用datetime模块

#convert string to datetime object
datetime_object = datetime.datetime.strptime("2018-08-14", '%Y-%m-%d')

# check if now is within 7 days of the above string
if datetime.datetime.now()-datetime_object<datetime.timedelta(days=7):
    print True

相关问题 更多 >