这个看似简单的问题困扰了我好几个小时。我想将以下字符串转换为分钟。(如果可以的话,也可以是几小时几分钟)
foo['stringtime'] = pd.Series(['1 hour and 59 minutes','2 hours', np.nan, '38 minutes', '4 hours and 31 minutes'])
#What I've tried:
foo['stringtime'] = foo['stringtime'].str.replace(r'hours?','').str.replace(' minutes','').str.split(' and ')
然而,这将造成一种情况,即'2 hours'
和'38 minutes'
变成['2']
和['38']
#What I would like to happen:
foo.head()
output:
119
120
NaN (or 0)
38
271
有什么优美优雅的Python式方法可以做到这一点吗
尝试使用正则表达式
Ex:
输出:
另一种方法可能只是使用
numexpr
来计算数值方程:输出:
相关问题 更多 >
编程相关推荐