pandas 将日期字符串转换为数字,并截取小时或者任意时间维度

>>> haha = pd.DataFrame({'时间':['2017-01-01 13:15:00','2017-01-01 01:30:00','2017-01-01 04:45:00']})
>>> haha['时间'] = haha['时间'].str.slice(11,13,1)
>>> haha['时间']
0    13
1    01
2    04
Name: 时间, dtype: object
>>> haha.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 1 columns):
时间    3 non-null object
dtypes: object(1)
memory usage: 152.0+ bytes
>>> haha['时间'] = haha['时间'].astype(float)
>>> haha['时间']
0    13.0
1     1.0
2     4.0
Name: 时间, dtype: float64
>>> haha['时间'] = haha['时间']/10
>>> haha['时间']
0    1.3
1    0.1
2    0.4
Name: 时间, dtype: float64
>>> 

 

    原文作者:greatcoti
    原文地址: https://blog.csdn.net/qq_30331643/article/details/100104891
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞