Grasper
Grasper

Reputation: 31

Converting a column containing string time format into minutes

I contain a time data frame that has time in this format:

df = pd.DataFrame({'Time': ['20hours 10minutes', '18hours 10minutes',
                            '15minutes', '1minutes']})
                Time
0  20hours 10minutes
1  18hours 10minutes
2          15minutes
3           1minutes

I would like to convert the entire column into minutes. Any suggestions? My problem is that later on, it is just minutes left and no hours, so whatever I try I can't get it right

Upvotes: 3

Views: 166

Answers (1)

SeaBean
SeaBean

Reputation: 23217

You can use the pd.to_timedelta() to parse the time strings and use pd.Timedelta.total_seconds() to get the total seconds for conversion to minutes, as follows:

df['minutes'] = pd.to_timedelta(df['Time']).dt.total_seconds() / 60

Result:

                Time  minutes
0  20hours 10minutes   1210.0
1  18hours 10minutes   1090.0
2          15minutes     15.0
3           1minutes      1.0

Upvotes: 3

Related Questions