I am working with spacy package v3.2.1 in Python 3.9 and wanted to understand how I can use it to remove names from a data frame. I tried following the spacy documentation and I am able to identity names correctly, but not understanding how I can remove them. My goal is to remove all names from a specific column of the data frame.
Actual
| ID | Comment |
|---|---|
| A123 | I am five years old, and my name is John |
| X907 | Today I met with Dr. Jacob |
What I am trying to accomplish
| ID | Comment |
|---|---|
| A123 | I am five years old, and my name is |
| X907 | Today I met with Dr. |
Code:
#loading packages
import spacy
import pandas as pd
from spacy import displacy
#loading CSV
df = pd.read_csv('names.csv)
#loading spacy large model
nlp = spacy.load("en_core_web_lg")
#checking/testing is spacy large is identifying named entities
df['test_col'] = df['Comment'].apply(lambda x: list(nlp(x).ents))
What my code does
| ID | Comment | test_col |
|---|---|---|
| A123 | I am five years old, and my name is John | [(John)] |
| X907 | Today I met with Dr. Jacob | [(Jacob)] |
But how do I go from removing those names from the Comment column? I think I some sort of function that iterates over each row of the data frame and removes the identified entities. Would appreciate your help
Thank you