python - Performing regex extraction with apply() on pandas dataframe column uses result of first row for all rows -
i have column of time zone strings in pandas dateframe
each string entry variation on following:
'local time zone (america/chicago (cst) offset -21600)'
i trying extract numeric offset (listed in seconds) @ end of string apply
call:
df['minuteoffset'] = df.timezone.apply(lambda x: int(re.match('.*?offset (-?[0-9]*)\\)', a).group(1)))
however, when @ data frame, see first value -21600 carried forward rows though other rows have other values. how can correctly extract regex on per row basis generate new column , why above failing?
i way:
in [85]: in [82]: df out[85]: id timezone 0 1 local time zone (america/chicago (cst) offset -21600) 1 2 local time zone (kiev/ukraine (eest) offset +10800) in [86]: df['minuteoffset'] = df.timezone.str.replace(r'.*offset\s+([\+\-\d+]+)\)', r'\1').astype(int)/60 in [87]: df out[87]: id timezone minuteoffset 0 1 local time zone (america/chicago (cst) offset -21600) -360.0 1 2 local time zone (kiev/ukraine (eest) offset +10800) 180.0