【问题标题】:How do I write a relevant REGEX pattern to extract sub-string of a larger text string in python如何编写相关的 REGEX 模式以在 python 中提取较大文本字符串的子字符串
【发布时间】:2017-06-27 16:37:49
【问题描述】:

我有一个数据框data(带有冗长且不一致的文本字符串注释)和匹配的 ID。我的目标是使用子字符串的list 提取感兴趣的相关子字符串,并为提取的子字符串创建一个新列。有人告诉我,正则表达式是一个很好的起点,但我还没有想出一个可以产生匹配结果的好模式。我希望有人看到这个并指导我以正确的方式解决这个问题。

list = ['sentara williamsburg regional medical',
       'shady grove adventist hospital',
       'sibley memorial hospital',
       'southern maryland hospital center',
       'st. mary`s hospital',
       'suburban hospital healthcare system',
       'the cancer center at lake manassas',
       'ucla medical center',
       'united medical center- greater southeast community',
       'univ of md charles regional medical ctr',
       'university of maryland medical center',
       'university of north carolina hospital',
       'university of virginia health system',
       'unknown facility',
       'va medical center',
       'virginia hospital center-arlington',
       'walter reed army medical center',
       'washington adventist hospital',
       'washington hospital center',
       'wellstar health system, inc',
       'winchester medical center']

 data:
     ID     Notes                             
     530.0  Cancer is best diag @Wwashington Adventist Hospital
     651.0  nan
     692.0  GMC-009 can be accessed at ST. Mary`s but not in UCLA Med. Center
     993.0  I'm not sure of Sibley; however, Shady Grove Adventist Hosp. is great hospital
     044.0  nan
     055.0  2015-01-20 was the day she visited WR Army Medical Center in WDC
     476.0  nan

预期输出 - 大小写无关紧要!

 data_out: 
     ID     Notes                             
     530.0  Washington Adventist Hospital
     651.0  nan
     692.0  ST. Mary`s Hospital, UCLA Medical Center
     993.0  Sibley Memorial Hoapital, Shady Grove Adventist Hospital
     044.0  nan
     055.0  Walter Reed Army Medical Center
     476.0  nan

【问题讨论】:

  • 预期输出是什么?
  • 刚刚用预期的输出更新了问题

标签: python regex string pandas dataframe


【解决方案1】:

更新:此代码遍历列表中的所有单词并将它们与“注释”列进行比较。如果有一个单词在“list”和“Notes”中,这个单词将被写在新的“output”列中。您必须使用正则表达式才能获得所需的结果。笔记: 由于“列表”中的单词可能看起来完全不同但与“列”中的单词具有相同的含义(缩写,拼写,错误,Csase-sensitive),因此很难获得所有不同的情况。因此,也许用“词袋方法”解决这个问题会很有用......

#Create a new list
newlist=[]

#Split the sentences of the "Notes" column
[newlist.append(data.loc[i,"Notes"].split(" ")) for i in range(len(data["Notes"]))]

#Create the new column "output" and default the values to be the same as in the column "Notes"
data["output"]=data["Notes"]

#Run through all words
for i in range(len(list)):
    for j in range(len(newlist)):
        for element in range(len(newlist[j])):
            if re.search(newlist[j][element],list[i]):
                data.loc[j,"output"]= "' '{0}".format(newlist[j][element])

如果有更矢量化的方法,我会感谢 cmets

【讨论】:

  • 谢谢@user8144295 我在最后一个for循环中仍然有错误Invalid Synthax:[[data.loc[j,data[' Notes']]=list[i] if list[i] in data.loc[j,data['Notes']] for i in range(len(list))]for j in range(len(data['Notes '])) ] 如果您不介意,请帮我检查一下。再次感谢您。
  • 这太棒了。将在几个小时内试用并给您反馈。谢谢@user8144295
【解决方案2】:

我会做某事。喜欢:

import re
reg = re.compile('|'.join(your_list))
results = reg.match(your_data)

【讨论】:

  • reg = re.compile('|'.join(locations)) data['Location'] = data.match(reg) ****Error: 'DataFrame' object has no attribute ' match' 另外: reg = re.compile('|'.join(locations)) data['Location'] = data['Notes'].match(reg) ****Error: Series object has no attribute 'match '。我该如何解决。谢谢!
  • 是的..对不起..混淆了两者...我编辑了我的答案。
  • 喜欢..reg.match(data['Notes'])
  • 得到了 str 和 byte-like objects 错误,但使用了这个代码 ****reg = re.compile('|'.join(locations)) data['Location'] = reg.match( str(data['Notes']).lower()) *** 修复所有错误。但是,仍然没有得到任何匹配的子字符串
猜你喜欢
  • 2023-01-07
  • 2022-08-19
  • 1970-01-01
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多