Python 对dataframe列执行带apply()的正则表达式提取时,所有行都使用第一行的结果
我在Python 对dataframe列执行带apply()的正则表达式提取时,所有行都使用第一行的结果,python,regex,dataframe,Python,Regex,Dataframe,我在熊猫日期框中有一列时区字符串,其中每个字符串条目都是以下内容的变体: 'Local Time Zone (America/Chicago (CST) offset -21600)' 我试图通过apply调用提取字符串末尾的数字偏移量(以秒为单位列出): df['minuteOffset'] = df.timezone.apply(lambda x: int(re.match('.*?offset (-?[0-9]*)\\)', a).group(1))) 然而,当我查看数据帧时,我看到第
熊猫日期框中有一列时区字符串,其中每个字符串条目都是以下内容的变体:
'Local Time Zone (America/Chicago (CST) offset -21600)'
我试图通过apply
调用提取字符串末尾的数字偏移量(以秒为单位列出):
df['minuteOffset'] = df.timezone.apply(lambda x: int(re.match('.*?offset (-?[0-9]*)\\)', a).group(1)))
然而,当我查看数据帧时,我看到第一个值-21600结转所有行,即使其他行有其他值如何正确提取每行正则表达式以生成新列,以及为什么上述操作失败?我会这样做:
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
当你打算使用“x”时,你在正则表达式中使用过变量“a”吗?@helloB,有用吗?