【发布时间】:2018-08-17 11:50:43
【问题描述】:
我有一个 df 列,其中包含文本,我正在尝试从中提取不同的日期模式。
这个df1 例如:
<index> text
0 My birthday is 10/23/89.
1 Christmas is on December 25th.
2 Thanksgiving of 11/2008 was the best.
所需的输出是第三列,称为dates:
<index> text dates
0 My birthday is 10/23/89. 10/23/89
1 Christmas is on December 25. 25 December
2 Thanksgiving of 11/2008 was the best. 11/2008
为了拉出我们的第一次约会,我写了我的第一个 re 表达式,像这样:
df1['dates'] = (df1['text'].str.findall(r'\d{1,2}[/-]\d{1,2}[/-]\d{2 ,4}'))
这就是我卡住的地方。
我不知道/不明白如何编写多个 re 表达式,而不是继续写 df1['dates'] 列中已有的内容。
我想运行下一个表达式:
df1['dates'] = df1['text'].str.findall(r'(?:\d{1,2})?(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]* (?:\d{1,2}, )?\d{4}')
如何或最好的方法是查看df['dates'] 列是否为空,然后尝试下一个 re 表达式?
我今天早些时候问过这个问题,它被标记为可能与 this 重复,但我认为 DeepSpace 认为我比实际聪明得多,我的问题比他回答的问题更基本。
【问题讨论】:
标签: python regex pandas dataframe