In the dataframe, one of the columns is duration. It was given as a string.
index duration 1 1 hour, 2 minutes, 21 seconds 2 1 hour, 2 minutes, 26 seconds 3 1 hour, 2 minutes, 41 seconds 4 1 hour, 4 minutes, 39 seconds 5 1 hour, 42 seconds 6 6 minutes, 7 seconds 7 9 minutes, 7 seconds 8 9 minutes, 9 seconds 9 9 minutes, 9 seconds 10 9 minutes, 9 seconds
How can I convert this column into seconds?
Advertisement
Answer
Use pd.Timedelta
to parse each item:
df['duration'] = df['duration'].apply(pd.Timedelta).dt.total_seconds().astype(int)
Output:
>>> df duration 0 3741 1 3746 2 3761 3 3879 4 3642 5 367 6 547 7 549 8 549 9 549