python - 如何在 pandas python 的 isocalendar 函数中应用系列

标签 python pandas datetime

我有下表:

Time                     
2016-09-10T23:20:00.000000000
2016-08-10T23:20:00.000000000
2016-09-10T23:20:00.000000000
2017-09-10T23:20:00.000000000
2016-09-10T23:20:00.000000000

我希望使用 isocalender 来计算工作周数,所以有什么想法可以分享给我吗?

Time                               WW
2016-01-01T23:20:00.000000000      201601
2016-01-01T23:20:00.000000000      201601
2016-01-01T23:20:00.000000000      201601
2017-01-01T23:20:00.000000000      201701
2018-01-01T23:20:00.000000000      201801

最佳答案

您可以使用:

#convert column to datetime
df['Time'] = pd.to_datetime(df['Time'])

#simplier solution with strftime
df['WW'] = df['Time'].dt.strftime('%G-%V')
#solution with isocalendar
df['WW1'] = df['Time'].apply(lambda x: str(x.isocalendar()[0]) + '-' + 
                                       str(x.isocalendar()[1]).zfill(2))
print (df)
                 Time       WW      WW1
0 2017-01-01 00:00:00  2016-52  2016-52 <- changed datetime
1 2016-08-10 23:20:00  2016-32  2016-32
2 2016-09-10 23:20:00  2016-36  2016-36
3 2017-09-10 23:20:00  2017-36  2017-36
4 2016-09-10 23:20:00  2016-36  2016-36

感谢@Fierr 将 '%Y-%V' 改成 '%G-%V'

关于python - 如何在 pandas python 的 isocalendar 函数中应用系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48058304/

相关文章:

c# - 使用 Json.Net 序列化时指定自定义 DateTime 格式

python - 使用 Pandas 在单独的列中标记重复项

python - HAPI FHIR 患者 bundle 请求

python - 在字典中删除重复列表的最快方法

python - Pandas 将函数应用于组,并过滤​​原始数据框

python - 对 pandas 列中字符串类型的值进行排序

python - 我将如何在 python 中提取水平和垂直格式化的数据?

c# - 如何在 MySQL 中舍入 DateTime?

python - Webassets - 排除 bundle 中的文件

python - 在python中解析CEST/CET时间