Mean value of the time
I tried df[].mean() method, but it is not working. There should be another changing to use it, I can't find it
Mean value of the time
I tried df[].mean() method, but it is not working. There should be another changing to use it, I can't find it
You can make use of strptime
to parse them into a datetime
object, then construct a timedelta
using the value parsed by datetime
.
AFter that, sum the values up and divide by the length to get the mean.
from datetime import datetime, timedelta
def stringToTimeDelta(str):
dt = datetime.strptime(str, "%H:%M:%S.%f")
return timedelta(hours=dt.hour, minutes=dt.minute, seconds=dt.second, microseconds=dt.microsecond)
input = ["0:00:34.805936", "0:01:17.946625", "0:01:41.294841"]
# Convert string to timedelta
parsed = [stringToTimeDelta(x) for x in input]
# Maths operation on timedelta
print(sum(parsed, timedelta(0)) / len(parsed))
Convert the column to a timedelta
first:
import pandas as pd
df = pd.DataFrame({'elapsed': ['0:00:34.805936', '0:01:17.946625', '0:01:41.294841']})
df.elapsed = pd.to_timedelta(df.elapsed)
print(df.elapsed.mean())
Output:
0 days 00:01:11.349134
A much simpler solution could be to use the .split() function.
time = "0:01:17.946625"
some_list = time.split(":")
This splits the string whenever it sees ":". The list is now: ["0", "01", "17.946625"] Then, use a list comprehension to convert them to integers.
int_list = [int(item) for item in some_list]
Now, the list is [0, 1, 17.946625] Then, you can convert this list into seconds.
hours_to_seconds = int_list[0] * 3600
mins_to_seconds = int_list[1] * 60
seconds = hours_to_seconds + mins_to_seconds + int_list[2]
Now, you have seconds. You can calculate the mean by storing all the seconds in a list.
time_list = [val1, val2, val3] # this list should have the seconds that you calculated
average = sum(time_list) / len(time_list)
Now, you have the average time. Please keep in mind that this is an extremely easy and inefficient way to get the average time, and other answers will definitely be more complex.