Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python remove title from name

import re
'''
   # Word boundary (to match 'mr' but not 'zmr' unless specified)
(?:group|of|prefixes|that|we|want|to|remove) # example
.   # Literal '.'
s*  # 0 or more spaces
'''
# Ex1
prefixes = ['mr', 'smr']
regex = r'(?:' + '|'.join(prefixes) + r').s*'
i = 'hi mr.john, smr. john, etc. Previous etc should not be removed'
i = re.sub(regex,'',i)
print(i)

# Ex2
As a regular expression this looks like r'^w{2,3}. ?.'
Now you can use re.sub to replace this part with an empty string.

cleaned_name = re.sub(r'(^w{2,3}. ?)', r'', name)
# Assert that full_name has no honorifics
assert df['full_name'].str.contains('Ms.|Mr.|Miss|Dr.').any() == False
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #python #remove #title
ADD COMMENT
Topic
Name
3+5 =