【发布时间】:2018-10-10 22:46:10
【问题描述】:
我有一个包含文本的列(文本行)的数据框 df
df['textline'].iloc[0] = 'This is a test with 2018\n'
df['textline'].iloc[1] = 'This is a test with Jan 2018\n'
df['textline'].iloc[2] = 'This is a test with Feb 2018\n'
我想使用 Regex extractall 来遍历整个 df['textline'] 但它只会在没有前面的月份名称时提取年份。例如,在上面的示例中,它将从第一行中提取 2018 年,但不会从第二行或第三行中提取 2018 年,因为它有 Jan 或 Feb(或其他月份)。
df['textline'].str.extractall(r'<<Regex code>>')
【问题讨论】:
-
这就是我要找的:正则表达式模式来做到这一点。
-
另外,请扩展正则表达式代码以不提取内容为 2018 年 1 月、2018 年 1 月等内容的行。我只想要纯 2018(无月份)的行
-
例如这是尝试: df['textline'].str.extractall(r' ^(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]* (\d{4})\D')