python - 如何使用 Pandas 在年份变化时继续周数

标签 python python-3.x pandas

示例:通过使用

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/

相关文章:

python - 使用 Flask 格式化电话号码以进行演示和数据库输入

python-3.x - 在 Heroku 上使用 Django 运行 PostGIS

python - 在不使用 eval 函数的情况下将 json 文件中的算术运算应用于数据框

python - 与 numpy 的 np.random.RandomState 和 Python 的 random.Random 相互转换?

python - 使用 virtualenv 在 debian 中守护 python 脚本

python - 如果 csv 为空,如何不读取 csv

c++ - boost_python with python 3.5 from anaconda - (few) undefined references

python - 用它的神奇行为掩盖内置变量?

来自云托管 jupyter 的 pandas.read_clipboard?

python - 当模型是 pandas dataframe 时使 QTableView 可编辑