Search code examples
pythonpandasdata-analysis

Pandas: Convert time into seconds for all values in column


I have a dataframe that represents time as minutes and seconds with the format mm:ss.ms or 00:00.00. I need to convert the entire column of values into seconds with dtype float. The dataframe column is shown below:

resultsorig['fastestLapTime']
Out[41]: 
0        01:27.5
1        01:27.7
2        01:28.1
3        01:28.6
4        01:27.4
  
24735    01:21.8
24736    01:22.5
24737    01:22.0
24738    01:20.4
24739    01:24.0
Name: fastestLapTime, Length: 24740, dtype: object

Everything I have found hasn't worked.

UPDATE: I've tried the following in the past and it has worked, but it's not working for this dataframe and I'm not sure why:

resultsorig=resultsorig[~resultsorig['fastestLapTime'].str.contains(":")]
resultsorig['fastestLapTime']=pd.to_numeric([resultsorig['fastestLapTime'])

Solution

  • try this..

    df['fastestLapTime']=df['fastestLapTime'].apply(lambda x: float(x.split(':')[0])*60+float(x.split(':')[1]))