[英]How to combine 3 separate columns of year(2 digit) ,month and day into single date column
I am combining 3 seoerate columns of year,month and day into a single column of my dataframe. 我将年,月和日的3个seoerate列合并到数据框的单个列中。 But the year is in 2 digit which is giving error.
但是年份以两位数表示,这是错误的。
I have tried to_datetime()
to do the same in jupyter notebook 我试图
to_datetime()
在jupyter笔记本中做同样的事情
Dataframe is in this form: 数据框的格式如下:
Yr Mo Dy RPT VAL ROS KIL SHA BIR DUB CLA MUL CLO BEL
61 1 1 15.04 14.96 13.17 9.29 NaN 9.87 13.67 10.25 10.83 12.58 18.50
61 1 2 14.71 NaN 10.83 6.50 12.62 7.67 11.50 10.04 9.79 9.67 17.54
61 1 3 18.50 16.88 12.33 10.13 11.17 6.17 11.25 NaN 8.50 7.67 12.75
data.rename(columns={'Yr':'Year','Mo':'Month','Dy':'Day'},inplace=True)
data['Date']=pd.to_datetime(data[['Year','Month','Day']],format='%y%m%d')
The error i am getting is: 我得到的错误是:
cannot assemble the datetimes: time data 610101 does not match format '%Y%m%d' (match)
无法组合日期时间:时间数据610101与格式'%Y%m%d'不匹配(匹配)
There is problem to_datetime
with specify columns ['Year','Month','Day']
need YYYY
format, so need alternative solution, because year is YY
only: 指定列
['Year','Month','Day']
to_datetime
问题需要YYYY
格式,因此需要替代解决方案,因为year仅是YY
:
s = data[['Yr','Mo','Dy']].astype(str).apply('-'.join, 1)
data['Date'] = pd.to_datetime(s, format='%y-%m-%d')
print (data)
Yr Mo Dy RPT VAL ROS KIL SHA BIR DUB CLA MUL \
0 61 1 1 15.04 14.96 13.17 9.29 NaN 9.87 13.67 10.25 10.83
1 61 1 2 14.71 NaN 10.83 6.50 12.62 7.67 11.50 10.04 9.79
2 61 1 3 18.50 16.88 12.33 10.13 11.17 6.17 11.25 NaN 8.50
CLO BEL Date
0 12.58 18.50 2061-01-01
1 9.67 17.54 2061-01-02
2 7.67 12.75 2061-01-03
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.