I need to slice a pandas DataFrame based on spacy rule-based matcher results. The following is what I tried.
import pandas as pd
import numpy as np
import spacy
from spacy.matcher import Matcher
df = pd.DataFrame([['Eight people believed injured in serious SH1 crash involving truck and three cars at Hunterville',
'Fire and emergency responding to incident at Mataura, Southland ouvea premix site',
'Civil Defence Minister Peeni Henare heartbroken over Northland flooding',
'Far North flooding: New photos reveal damage to roads']]).T
df.columns = ['col1']
nlp = spacy.load("en_core_web_sm")
flood_pattern = [{'LOWER': 'flooding'}]
matcher = Matcher(nlp.vocab, validate=True)
matcher.add("FLOOD_DIS", None, flood_pattern)
titles = (_ for _ in df['col1'])
g = (d for d in nlp.pipe(titles) if matcher(d))
x = list(g)
df2 = df[df['col1'].isin(x)]
df2
This produces an empty DataFrame. However, It should extract the following two rows from df.
- Civil Defence Minister Peeni Henare heartbroken over Northland flooding
- Far North flooding: New photos reveal damage to roads