I have a df and a column with strings that looks like following:
runtime
1h 38m
20h 4m
5h
45m
empty
and I am trying to apply a function which will convert it to minutes.
So far, I have come up with part of it:
def runtime_to_minutes(string):
try:
capt_numbers = re.compile(r'[\d+][\d+]')
hours = int(re.findall(capt_numbers, string)[0])
minutes = int(re.findall(capt_numbers, string)[1])
duration = hours * 60 + minutes
return duration
except Exception as error:
return str(error)
which obviously cannot handle all the situations, although it won't work for '1h 38m' either as I get an error list index out of range when I do: df['minutes'] = df['runtime'].apply(lambda s: runtime_to_minutes(s))
How should I restructure the regex and the function to get the desired result?