python - 如何在python中将一串小时和分钟转换为分钟?
问题描述
我的数据框 df 中有一个列:
Time
2 hours 3 mins
5 hours 10 mins
1 hour 40 mins
我想在 df 'Minutes' 中创建一个新列,将此列转换为分钟
Minutes
123
310
100
有没有一个python函数可以做到这一点?
解决方案
你需要通过转换它to_datetime
s=pd.to_datetime(df.Time.replace({'hours':'hour'},regex=True),format='%H hour %M mins')
s.dt.hour*60+s.dt.minute
Out[406]:
0 123
1 310
2 100
Name: Time, dtype: int64
或者我们使用str.findall
withnumpy
dot
np.dot(np.array(df.Time.str.findall('\d+').tolist()).astype(int),[60,1])
Out[420]: array([123, 310, 100])
推荐阅读
- android - 协程中的异步函数在 Android 上不起作用
- sql - SQL - 检查是否在另一个表中找到唯一值
- regex - 使用 Google RE2 Regexp for Data Studio 从 URL 中提取域名
- macos - 无法在 Mac 上的系统启动时运行 shell 脚本?
- tensorflow - 我应该使用哪种 TensorFlow 来预测某人是否会喜欢某个地方?
- jenkins - Jenkins 定期构建表达式“*/40 * * * *”未按预期工作
- python - 安装包后Python中的请求不起作用
- cloud-foundry - 如何轻松地从带有目的地的 html5 应用程序中使用 nodejs-backend?
- sql - 如何根据返回的记录数选择列?
- redux - 如何在 Flutter 的 Redux Store 中存储变量?