我有以下数据框:

sale_id      created_at
1               2016-05-28T05:53:31.042Z
2               2016-05-30T12:50:58.184Z
3               2016-05-23T10:22:18.858Z
4               2016-05-27T09:20:15.158Z
5               2016-05-21T08:30:17.337Z
6               2016-05-28T07:41:14.361Z

我需要添加一个年-周列,其中包含创建列中每行的年数和周数:
sale_id      created_at                      year_week
1               2016-05-28T05:53:31.042Z       2016-21
2               2016-05-30T12:50:58.184Z       2016-22
3               2016-05-23T10:22:18.858Z       2016-21
4               2016-05-27T09:20:15.158Z       2016-21
5               2016-05-21T08:30:17.337Z       2016-20
6               2016-05-28T07:41:14.361Z       2016-21

最佳答案

您可以使用strftime

#if dtype is not datetime
df.created_at = pd.to_datetime(df.created_at)

df['year_week'] = df.created_at.dt.strftime('%Y-%U')
print (df)
   sale_id              created_at year_week
0        1 2016-05-28 05:53:31.042   2016-21
1        2 2016-05-30 12:50:58.184   2016-22
2        3 2016-05-23 10:22:18.858   2016-21
3        4 2016-05-27 09:20:15.158   2016-21
4        5 2016-05-21 08:30:17.337   2016-20
5        6 2016-05-28 07:41:14.361   2016-21

另一个Python's strftime directivesdt.year的解决方案
df['year_week'] = df.created_at.dt.year.astype(str) + '-' +
                  df.created_at.dt.week.astype(str)
print (df)
   sale_id              created_at year_week
0        1 2016-05-28 05:53:31.042   2016-21
1        2 2016-05-30 12:50:58.184   2016-22
2        3 2016-05-23 10:22:18.858   2016-21
3        4 2016-05-27 09:20:15.158   2016-21
4        5 2016-05-21 08:30:17.337   2016-20
5        6 2016-05-28 07:41:14.361   2016-21

08-20 03:51