使用 Python 获取列中的周数
我试图根据数据在数据范围内使用一周的数字,根据数据中的几天,一个月的时间里有一周的数字。 这是以下代码 -
import pandas as pd
import numpy as np
s={'yr':[2014,2014,2014,2014,2015,2015,2015,2015],'Month':['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug'],'Days':[12,24,11,22,10,15,22,3]}
p=pd.DataFrame(data=s)
import datetime
p['week']=datetime.date(p['yr'], p['Month'], p['Days']).isocalendar().week
I am getting the below error-
TypeError: cannot convert the series to <class 'int'>
我不确定如何解决此错误,我认为我也需要以数字格式转换月份还日期吗?
I was trying to get this requirement where I want to have a column in my dataframe with week number, based on Days, Month an yr present in data.
Here is the below code-
import pandas as pd
import numpy as np
s={'yr':[2014,2014,2014,2014,2015,2015,2015,2015],'Month':['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug'],'Days':[12,24,11,22,10,15,22,3]}
p=pd.DataFrame(data=s)
import datetime
p['week']=datetime.date(p['yr'], p['Month'], p['Days']).isocalendar().week
I am getting the below error-
TypeError: cannot convert the series to <class 'int'>
I am not sure on how to resolve this error, I think I need to convert Month in numeric format too, how would be able to do this and get the week number in the date also?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这条线并不那么好,但它有效:
This line isn't as nice, but it works: