[英]convert string date to numeric for all values in column using pandas python
I need to transform "Jan 11, 2017 9:00 PM" to 2017-01-11 21:00:00 for all values in ColumnA. 对于ColumnA中的所有值,我需要将“2017年1月11日9:00 PM”转换为2017-01-11 21:00:00。
Is there an easy way / function for doing this in python using pandas? 使用pandas在python中有一个简单的方法/功能吗?
to_datetime
seems to handle this just fine: to_datetime
似乎处理这个很好:
In [6]:
pd.to_datetime('Jan 11, 2017 9:00 PM')
Out[6]:
Timestamp('2017-01-11 21:00:00')
So you can just overwrite your column using the output from this function: 所以你可以使用这个函数的输出覆盖你的列:
df['ColumnA'] = pd.to_datetime(df['ColumnA'])
And just to clarify that the dates and months are correct here: 只是为了澄清这里的日期和月份是正确的:
In [8]:
pd.to_datetime('Jan 11, 2017 9:00 PM').day
Out[8]:
11
In [9]:
pd.to_datetime('Jan 11, 2017 9:00 PM').month
Out[9]:
1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.