Python/Pandas-如何在时间持续时间内进行计算?
我的数据具有一个“ duration_time”
,每行的持续时间(ex:10:58
)包含分钟和秒。
我该如何计算此“ duration_time”
列(ex:sum sum以获取总持续时间,平均持续时间等?)
是什么dtype是什么最适合此类型的时间计算?
我的列的dtype
列出为对象
,并且不允许我执行计算。
预先感谢您的帮助!
My data has a column "duration_time"
with time durations for each row (ex: 10:58
) containing minutes and seconds.
How can I do calculations of this "duration_time"
column (for ex: sum all to get total duration time, mean duration time, etc?)
What Dtype is best suited for this type of time calculations?
The Dtype
of my column is listed as an object
and doesn't let me perform calculations.
Thank you in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
熊猫确实有一种时间代表,旨在表示持续时间。
您将必须进行一些解析,但是Pandas中有一个
to_timedelta
功能可以帮助您(您可能会发现这更适合您想做的计算。
Pandas does have a Timedelta type that is intended to represent durations.
You will have to do some parsing, but there is a
to_timedelta
function in pandas that will help you (reference here).You may find this more suited to the kind of calculation you want to do.
duration_time
列当前是“对象” dtype,这意味着它存储为字符串。为了使其可以使用数学,您需要提取数字值。下面的代码以该格式帐户持续时间,并在秒内返回数值:The
duration_time
column is currently an 'object' dtype, meaning it's stored as strings. To make it so you can do math with it, you need to extract the numeric values. The code below accounts for durations in that format and returns the numeric value in seconds: