I have a Pandas data frame column containing elements similar to the string McNally, King (XYZ). I would like to keep the last name, first name and remove everything else. Therefore after cleaning McNally, King (XYZ) should be McNally, King.
I have tried following two functions but not getting the desired result:
df['name'] = df['name'].str.extract(r'\w+\,\s[A-Z][a-z]+', expand=False)
df['name'] = df['name'].replace({r'\w+\,\s[A-Z][a-z]+' : r'\w+\,\s[A-Z][a-z]+'}, regex=True)
Second code replaces the substring with the regex itself, while the first code extracts the names from the string but I want to keep the name and remove everything else followed by the name.
Edit: Sample data:
Reyes, Rebecca L (XYZ)
Derry, Odd P (XYZ)
Garza, Per-Laura A (MNP)
Fernandez, Rafael Carl (XYZ)
Expected output:
Reyes, Rebecca
Derry, Odd
Garza, Per-Laura
Fernandez, Rafael
I would like to edit-in-place i.e. modify the existing datafame itself and not creating a new one.