如何计算票务年龄明智的 Pandas 数据帧

How to Calculate Ticket Age wise Pandas Dataframe

提问人:balaji 提问时间:11/17/2023 更新时间:11/17/2023 访问量:40

问:

我有一个这样的数据帧:

**Created Time**
2023-11-01 23:53:56
2023-11-01 22:32:02
2023-11-01 21:51:56
2023-10-18 12:19:46
2023-10-18 10:35:36
2023-10-17 10:27:21
2023-10-17 13:38:50
2023-10-17 11:44:44
2023-10-17 16:44:29
2023-10-17 11:54:54
2023-10-02 19:57:10
2023-10-02 17:43:55
2023-10-02 15:07:20
2023-10-02 11:35:39
2023-10-02 14:47:03
2023-08-03 17:08:57
2023-08-02 15:20:19
2023-08-02 14:50:45
2023-07-31 10:24:35
2023-07-29 14:48:46
2023-05-05 11:28:30
2023-05-05 16:37:14
2023-05-05 10:18:40
2023-05-05 10:38:03
2023-05-04 14:31:12
2022-10-29 16:19:22
2022-10-28 16:08:44
2022-10-27 11:25:03
2022-10-25 17:51:59
2022-10-23 11:23:57
2022-10-23 11:23:57

如您所见,上面是创建时间列,我想要下面格式化我的数据帧。

Ticket Age   Created Time
1-15 Days   2023-11-01 23:53:56
1-15 Days   2023-11-01 22:32:02
1-15 Days   2023-11-01 21:51:56
1-15 Days   2023-10-18 12:19:46
1-15 Days   2023-10-18 10:35:36
16-30 Days  2023-10-17 10:27:21
16-30 Days  2023-10-17 13:38:50
16-30 Days  2023-10-17 11:44:44
16-30 Days  2023-10-17 16:44:29
16-30 Days  2023-10-17 11:54:54
1 - 3 Months    2023-10-02 19:57:10
1 - 3 Months    2023-10-02 17:43:55
1 - 3 Months    2023-10-02 15:07:20
1 - 3 Months    2023-10-02 11:35:39
1 - 3 Months    2023-10-02 14:47:03
3 - 6 Months    2023-08-03 17:08:57
3 - 6 Months    2023-08-02 15:20:19
3 - 6 Months    2023-08-02 14:50:45
3 - 6 Months    2023-07-31 10:24:35
3 - 6 Months    2023-07-29 14:48:46
6 Months - 1Yr  2023-05-05 11:28:30
6 Months - 1Yr  2023-05-05 16:37:14
6 Months - 1Yr  2023-05-05 10:18:40
6 Months - 1Yr  2023-05-05 10:38:03
6 Months - 1Yr  2023-05-04 14:31:12
More than 1Yr   2022-10-29 16:19:22
More than 1Yr   2022-10-28 16:08:44
More than 1Yr   2022-10-27 11:25:03
More than 1Yr   2022-10-25 17:51:59
More than 1Yr   2022-10-23 11:23:57
More than 1Yr   2022-10-23 11:23:57

我需要新票年龄和年龄 1-15 天、16-30 天、1-3 个月、6 个月 -1 年、超过 1 年

如何添加年龄类别明智的年龄组

我该怎么做?

请指教。

熊猫

评论

2赞 Utkarsh 11/17/2023
你是如何计算年龄的?当前日期创建时间?
0赞 Panda Kim 11/17/2023
参考日期是什么时候?有必要有这么长的例子吗?有什么理由不能缩短它吗?
0赞 balaji 11/17/2023
我的参考日期-今天....

答:

0赞 Utkarsh 11/17/2023 #1
from datetime import datetime
def getDaysCategory(date_a_string, referenceDate) :
    date_a = datetime.strptime(date_a_string, '%Y-%m-%d %H:%M:%S')
    days = abs((date_a - referenceDate).days)
    if (days <= 15) :
        return "1-15 Days"
    if (days >= 16 and days <= 30) :
        return "16-30 Days"
    if (days >= 31 and days <= 90) :
        return "1-3 Months"
    if (days >= 91 and days <= 180) :
        return "3-6 Months"
    if (days >= 181 and days <= 365) :
        return "6 Months-1 Yr"
    if (days > 365) :
        return "More than 1Yr"

today = datetime.today()
for date in dataframe :
    days = getDaysCategory(date, today)

dataframe是您的 DataFrame 对象,只需遍历它,您就会得到您的日期类别。

评论

0赞 balaji 11/18/2023
谢谢你的评论,但不起作用
0赞 Utkarsh 11/20/2023
你能更具体地说明什么不起作用吗?您是否遇到任何错误,或者输出是意外的?
0赞 balaji 11/21/2023
ValueError:时间数据“创建时间”与格式“%Y-%m-%d %H:%M:%S”不匹配
0赞 Utkarsh 11/21/2023
上面的格式对我有用,你确定你正确地传递了每个日期,或者你能分享代码吗?
0赞 balaji 11/27/2023
如何共享代码....
1赞 jezrael 11/27/2023 #2

使用 to_datetime 表示日期时间,现在减去时间并通过 Series.dt.days 获取以天为单位的时间增量,因此可以传递到第一个新列的 DataFrame.insert

df['Created Time'] = pd.to_datetime(df['Created Time'])

days = df['Created Time'].rsub(pd.Timestamp('now')).dt.days.abs()

bins = [0,15,30,90,180,365,np.inf]
labels=["1-15 Days","16-30 Days","1-3 Months","3-6 Months","6 Months-1 Yr","More than 1Yr"]
df.insert(0,'Ticket Age', pd.cut(days, bins=bins, labels=labels))

print (df)
       Ticket Age        Created Time
0      16-30 Days 2023-11-01 23:53:56
1      16-30 Days 2023-11-01 22:32:02
2      16-30 Days 2023-11-01 21:51:56
3      1-3 Months 2023-10-18 12:19:46
4      1-3 Months 2023-10-18 10:35:36
5      1-3 Months 2023-10-17 10:27:21
6      1-3 Months 2023-10-17 13:38:50
7      1-3 Months 2023-10-17 11:44:44
8      1-3 Months 2023-10-17 16:44:29
9      1-3 Months 2023-10-17 11:54:54
10     1-3 Months 2023-10-02 19:57:10
11     1-3 Months 2023-10-02 17:43:55
12     1-3 Months 2023-10-02 15:07:20
13     1-3 Months 2023-10-02 11:35:39
14     1-3 Months 2023-10-02 14:47:03
15     3-6 Months 2023-08-03 17:08:57
16     3-6 Months 2023-08-02 15:20:19
17     3-6 Months 2023-08-02 14:50:45
18     3-6 Months 2023-07-31 10:24:35
19     3-6 Months 2023-07-29 14:48:46
20  6 Months-1 Yr 2023-05-05 11:28:30
21  6 Months-1 Yr 2023-05-05 16:37:14
22  6 Months-1 Yr 2023-05-05 10:18:40
23  6 Months-1 Yr 2023-05-05 10:38:03
24  6 Months-1 Yr 2023-05-04 14:31:12
25  More than 1Yr 2022-10-29 16:19:22
26  More than 1Yr 2022-10-28 16:08:44
27  More than 1Yr 2022-10-27 11:25:03
28  More than 1Yr 2022-10-25 17:51:59
29  More than 1Yr 2022-10-23 11:23:57
30  More than 1Yr 2022-10-23 11:23:57

评论

1赞 balaji 11/27/2023
非常感谢你,我真的很感谢你的帮助。工作正常 .............
1赞 balaji 11/27/2023
这真是太棒了:)谢谢你的解释,耶斯拉尔先生