[英]Time calculations, mean , median, mode
( ![]() |
Name![]() |
Gun_time ![]() |
Net_time![]() |
Pace![]() |
---|---|---|---|---|
John![]() |
28:48:00 ![]() |
28:47:00 ![]() |
4:38:00 ![]() |
|
George![]() |
29:11:00 ![]() |
29:10:00 ![]() |
4:42:00 ![]() |
|
Mike![]() |
29:38:00 ![]() |
29:37:00 ![]() |
4:46:00 ![]() |
|
Sarah![]() |
29:46:00 ![]() |
29:46:00 ![]() |
4:48:00 ![]() |
|
Roy![]() |
30:31:00 ![]() |
30:30:00 ![]() |
4:55:00 ![]() |
Q1. Q1。 How can I add another column stating difference between Gun_time and Net_time?
如何添加另一列说明 Gun_time 和 Net_time 之间的差异? Q2.
Q2。 How will I calculate the mean for Gun_time and Net_time.
我将如何计算 Gun_time 和 Net_time 的平均值。 Please help!
请帮忙!
I have tried doing the following but it doesn't work我已尝试执行以下操作,但它不起作用
df['Difference'] = df['Gun_time'] - df['Net_time'] df['差异'] = df['Gun_time'] - df['Net_time']
for mean value I tried df['Gun_time'].mean对于平均值,我尝试了 df['Gun_time'].mean
but it doesn't work either, please help!但它也不起作用,请帮助!
Q.3 What if we have times in 28:48 (minutes and seconds) format and not 28:48:00 the function gives out a value error. Q.3 如果我们有 28:48(分钟和秒)格式的时间,而不是 28:48:00,function 会给出一个值错误。
ValueError: expected hh:mm:ss format ValueError:预期的 hh:mm:ss 格式
Convert your columns to dtype timedelta, eg like将您的列转换为 dtype timedelta,例如
for col in ("Gun_time", "Net_time", "Pace"):
df[col] = pd.to_timedelta(df[col])
Now you can do calculations like现在您可以进行如下计算
df['Gun_time'].mean()
# Timedelta('1 days 05:34:48')
or或者
df['Difference'] = df['Gun_time'] - df['Net_time']
#df['Difference']
# 0 0 days 00:01:00
# 1 0 days 00:01:00
# 2 0 days 00:01:00
# 3 0 days 00:00:00
# 4 0 days 00:01:00
# Name: Difference, dtype: timedelta64[ns]
If you need nicer output to string, you can use如果你需要更好的 output 来串,你可以使用
def timedeltaToString(td):
hours, remainder = divmod(td.total_seconds(), 3600)
minutes, seconds = divmod(remainder, 60)
return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}"
df['diffString'] = df['Difference'].apply(timedeltaToString)
# df['diffString']
# 0 00:01:00
# 1 00:01:00
# 2 00:01:00
# 3 00:00:00
# 4 00:01:00
#Name: diffString, dtype: object
See also Format timedelta to string .另请参阅将 timedelta 格式化为字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.