У меня есть эти функции, вычисляющие значение временных строк в секундах.
Каков более короткий способ достичь этого с помощью встроенных pandas
методов?
import re
import math
data = pd.DataFrame(["54 minutes 45 seconds","3 hours 12 minutes","2 hours 7 minutes","15 minutes 12 seconds","51 minutes 35 seconds"], columns = ["Matching"])
def get_match(string,pattern):
if not string != string:
found = pattern.search(string)
if found != None:
value = re.compile("\d{1,}").search(found[0])
return int(value[0])
return 0
def convert_time(col):
for index, row in col.iteritems():
min_p = re.compile("\d{1,}\sminutes")
sec_p = re.compile("\d{1,}\sseconds")
hr_p = re.compile("\d{1,}\shours")
mins = get_match(row, min_p)
secs = get_match(row, sec_p)
hrs = get_match(row, hr_p)
time = mins * 60 + secs + hrs * 60 * 60
col[index] = time
convert_time(data["Matching"])
data.head()