我有一个嵌套的字符串列表,我想提取它们的日期。日期格式是:
两个数字(从
01
到12
)连字符树字母(一个有效月份)连字符两个数字,例如:08-Jan—07
或03-Oct—01
我尝试使用以下正则表达式:
r'\d{2}(—|-)(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2,4}'
然后,我对它进行了如下测试:
import pandas as pd
df = pd.DataFrame({'blobs':['6-Feb- 1 4 Facebook’s virtual-reality division created a 3-EBÚ7 11 network of 500 free demo stations in Best Buy stores to give people a taste of VR using the Oculus Rift 90 GT 48 headset. But according to a Wednesday report from Business Insider, about 200 of the demo stations will close after low interest from consumers. 17-Feb-2014',
'I think in a store environment getting people to sit down and go through that experience of getting a headset on and getting set up is quite a difficult thing to achieve,” said Geoff Blaber, a CCS Insight analyst. 29—Oct-2012 Blaber 32 FAX 2978 expects that it will get easier when companies can convince 18-Oct-12 credit cards. '
]})
df
然后:
df['blobs'].str.extractall(r'\d{2}(—|-)(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2,4}')
然而,他们并没有发挥作用。以前的regex没有给我任何东西(例如,只给出了-
):
Col
0 NaN
1 -
2 -
3 NaN
4 NaN
5 -
...
n -
如何修复它们以便获得?:
Col
0 6-Feb-14, 17-Feb-2014
1 29—Oct-2012, 18-Oct-12
更新
我还试图:
import re
df['col'] = df.blobs.apply(lambda x: re.findall('\d{2}(—|-)(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2,4}',x))
s = df.apply(lambda x: pd.Series(x['col']),axis=1).stack().reset_index(level=1, drop=True)
s.name = "col"
df = df.drop('col')
df
尽管如此,我还是得到了:
ValueError Traceback (most recent call last)
<ipython-input-4-5e9a34bd159f> in <module>()
3 s = df.apply(lambda x: pd.Series(x['col']),axis=1).stack().reset_index(level=1, drop=True)
4 s.name = "col"
----> 5 df = df.drop('col')
6 df
/usr/local/lib/python3.5/site-packages/pandas/core/generic.py in drop(self, labels, axis, level, inplace, errors)
1905 new_axis = axis.drop(labels, level=level, errors=errors)
1906 else:
-> 1907 new_axis = axis.drop(labels, errors=errors)
1908 dropped = self.reindex(**{axis_name: new_axis})
1909 try:
/usr/local/lib/python3.5/site-packages/pandas/indexes/base.py in drop(self, labels, errors)
3260 if errors != 'ignore':
3261 raise ValueError('labels %s not contained in axis' %
-> 3262 labels[mask])
3263 indexer = indexer[~mask]
3264 return self.delete(indexer)
ValueError: labels ['col'] not contained in axis
发布于 2017-02-15 21:20:28
使用Series.str.extract
或Series.str.extractall
时,将返回捕获的子字符串,而不是所有匹配的子字符串。因此,您需要确保捕获(即添加(
和)
)所需获取的模式部分。
现在,行中的几个预期匹配使得使用extractall
变得更加困难,如果模式中没有定义捕获组,您似乎可以使用Series.str.findall
返回整个匹配。
使用
rx = r'\b\d{1,2}[-–—](?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[-–—](?:\d{4}|\d{2})\b'
df['Col'] = df['blobs'].str.findall(rx).apply(','.join)
.apply(','.join)
将在Col
列中将列表转换为逗号分隔的字符串。
这种模式意味着:
\b
-一个单词边界\d{1,2}
-1或2位数[-–—]
-一个连字符,em-或en-破折号(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)
-12个月缩短的名字中的任何一个[-–—]
-一个连字符,em-或en-破折号(?:\d{4}|\d{2})
-4或2位数\b
-一个单词边界https://stackoverflow.com/questions/42254384
复制相似问题