I am trying to using following code to extract entities from text available in DataFrame.
for i in df['Text'].to_list():
doc = nlp(i)
for entity in doc.ents:
if entity.label_ == 'GPE':
I need to store text of first GPE
with it's corresponding column of text. Like for instance if following is text at index 0 in column df['Text']
Match between USA and Canada was postponed
then I need only first location(USA) in another column such as df['Place']
at the corresponding index to Text which is 0. df['Place']
is not already available in DataFrame means it will be created while assigning value. I have tried following code. But it fills whole column with very first value it can find.
for i in df['Text'].to_list():
doc = nlp(i)
for entity in doc.ents:
if entity.label_ == 'GPE':
df['Place'] = (entity.text)
I have also tried to append text in list with e_list.append((entity.text))
but it will append all entities it can find in text.
Can someone help that how can I store only first entity only at corresponding index. Thank you