简体   繁体   English

如何从 pandas 数据帧中的时间戳列中提取小时数并转换为浮点类型

[英]How to extract hours from time stamp column in pandas data frame and convert to float type

Hi I want to create a liner regression prediction model and want to get a time as a independent variable.您好我想创建一个线性回归预测 model 并希望将时间作为自变量。 My data frame has a time stamp and I wanted to extract the hours from it and put in to separate column as float data type.我的数据框有一个时间戳,我想从中提取小时数并将其作为浮点数据类型放入单独的列中。 Highly appreciate your support on this.非常感谢您对此的支持。 Thank you.谢谢你。 Time stamp 12/1/2021 8:39 7/10/2020 13:47 1/11/2020 11:53 4/10/2020 12:33 11/10/2020 12:40时间戳 12/1/2021 8:39 7/10/2020 13:47 1/11/2020 11:53 4/10/2020 12:33 11/10/2020 12:40

Use to_datetime with hour :使用to_datetimehour

#DD/MM/YYYY H:M
pd.to_datetime(df['Time stamp'], format='%d/%m/%Y %H:%M').dt.hour.astype(float)

#MM/DD/YYYY H:M
pd.to_datetime(df['Time stamp'], format='%m/%d/%Y %H:%M').dt.hour.astype(float)

Or extract digits between space and : :或提取空格和:之间的digits

df['Time stamp'].str.extract('\s+(\d+):').astype(float)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM