Home > Mobile >  Convert string duration column to seconds
Convert string duration column to seconds

Time:06-04

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?

CodePudding user response:

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

CodePudding user response:

Here is one solution:

import pandas as pd
df = pd.DataFrame()
l = ['1 second', '2 seconds', '3 seconds']
df['seconds'] = l
print(df)
df['seconds'] = [int(cell.replace('seconds', '').replace('second', '')) for cell in df['seconds']]
print(df)

Output:

Before:
     seconds
0   1 second
1  2 seconds
2  3 seconds
After:
  seconds
0      1 
1      2 
2      3 
  • Related