Pandas Converting an object into timedelta

こ雲淡風輕ζ 提交于 2019-12-22 08:06:22

问题


I have the following data

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

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.


回答1:


in case of series,

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

dataframe,

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


来源:https://stackoverflow.com/questions/36214105/pandas-converting-an-object-into-timedelta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!