I have a column in a dataframe like this.
Text
"Lorum Ipsum Rotterdam dolor sit."
"ed ut perspiciatis Boekarest, New York, consectetur adipiscing elit, sed "
"Excepteur sint occaecat Glasgow cupidatat non proident, sunt in culpa"
I want every geographical location to be replaced by "GPE".
I am using spacy to detect the entities. This works fine, as shown below.
nlp = spacy.load('en_core_web_lg')
for value in df['text']:
doc = nlp(value)
for ent in doc.ents:
print(ent.text, ent.label_)
Output:
Rotterdam GPE
Boekarest GPE
New York GPE
Glasgow GPE
I tried the code below in order to replace the city names within the columns, but it doesn't work.
for value in df['text']:
doc = nlp(value)
for ent in doc.ents:
for word in value.split():
if ent.label_ == "GPE":
word.replace(ent.label, "_GPE_")
Does anyone see what I am doing wrong?