这个问题有一个参考(here)。 我是Python的新手,因此陷入了一些微不足道的问题!我有一个数据系列如下
Text
0 some texts...qualifications: BE year of passing 2012
1 MCOM from XYZ University in 2007. In 2009 he obtained his MBA
2 Academics: University / Board: XYZ University year of passing:2014
目标是提取所提到的年份(仅限第一个),即2012,2007,2014
。现在我的方法如下:
corpus = pd.Series('the above series')
corpus = corpus.str.replace(r'^[A-Za-z0-9]+')
corpus = corpus.str.lower()
if corpus.str.contains('qualifications').any():
corpus.str.extract('.*qualifications.*?(\d{4})', expand = False)
if corpus.str.contains('university').any():
corpus.str.extract('.*university. *?(d\{4})', expand=False)
if corpus.str.contains('academics').any():
corpus.str.extract('.*academics. *?(d\{4})',expand=False)
上述方法正在创建一个空白系列。请帮助我解决这个问题。
答案 0 :(得分:2)
我认为你可以简化这个表达式:
corpus = pd.Series("""
some texts...qualifications: BE year of passing 2012
MCOM from XYZ University in 2007. In 2009 he obtained his MBA
Academics: University / Board: XYZ University year of passing:2014
""".split('\n')[1:-1], name='Text')
corpus = corpus.str.lower().str.extract(
'(university|academics|qualifications).*?(\d{4})', expand=False)
print(corpus)
0 1
0 qualifications 2012
1 university 2007
2 academics 2014
join -a 1 -a 2 a.txt <( awk '{print $1, "--", $0, "--"}' < b.txt ) | sed 's/ --$//' | awk -F ' -- ' '{print $NF}'