示例:通过使用
df['Week_Number'] = df['Date'].dt.strftime('%U')
for 29/12/2019 the week is 52. and this week is from 29/12/2019 to 04/01/2020.
but for 01/01/2020 the week is getting as 00.
我要求 01/01/2020 的一周也为 52。05/01/2020 到 11/01/2020 为 53。这需要继续。
最佳答案
如 converting a pandas date to week number 中所述,您可以使用 df['Date'].dt.week
获取周数。
为了让它继续下去,你也许可以用新的周值来总结上周的数字,像这样?我现在无法测试这个......
if(df['Date'].dt.strftime('%U') == 53):
last = df['Date'].dt.strftime('%U')
df['Week_Number'] = last + df['Date'].dt.strftime('%U')
关于python - 如何使用 Pandas 在年份变化时继续周数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59425176/