Pandas 将对象转换为 timedelta [英] Pandas Converting an object into timedelta

查看:60
本文介绍了Pandas 将对象转换为 timedelta的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据

   Duration
0  00:00:00  
1  00:00:00  
2  00:00:57  
3  00:03:16  
4  00:00:00  

并且 Duration 被存储为一个对象.我想将其转换为具有秒数的整数.例如 00:03:16 被转换为 196.我尝试了各种类似 astype(timedelta64[s]) 但没有成功.我尝试提取分钟和秒并尝试转换为整数,这也没有产生结果.我无法将提取的字符串转换为整数.

And Duration is stored as an object. I would like to convert this into an integer having seconds. for eg 00:03:16 gets converted into 196. I tried various things like astype(timedelta64[s]) but no success. I tried extracting the minutes and seconds and tried converting to integer, that also did not yield results. I am unable to convert the extracted string into an integer.

推荐答案

如果是系列,

s.map(lambda x: pd.to_timedelta(x).seconds)

数据框,

df.applymap(lambda x: pd.to_timedelta(x).seconds)

这篇关于Pandas 将对象转换为 timedelta的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆