Search
 
SCRIPT & CODE EXAMPLE
 

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
Comment

PREVIOUS NEXT
Code Example
Python :: scipy random seed 
Python :: IndexError: child index out of range in parsing xml for object detection 
Python :: pandas split coordinate tuple 
Python :: Encapsulation in Python using public members 
Python :: python record screen and audio 
Python :: series clip 
Python :: pade python 
Python :: How to pass a data frame as parameter to a SQL query in Python? 
Python :: Insert Multiple Images to Excel with Python 
Python :: python project pick text color according to background 
Python :: shorthand python if 
Python :: find a string hackereank 
Python :: pylint no name in module opencv 
Python :: how to convert string labels to numpy array 
Python :: Pte or Pvt 
Python :: linear algebra ipython notebook 
Python :: nlp.Defaults.stop_words.add spacy 
Python :: remove brackets from string python 
Python :: qq plot using seaborn 
Python :: python code to print fibonacci series 
Python :: django on_delete rules 
Python :: python using string to access objects 
Python :: access kwargs in template django 
Python :: convert string to double 2 decimal places python 
Python :: width and precision thousand separetor python 
Python :: django rest DjangoModelPermissions include get 
Python :: python urllib.request.urlretrieve with a progressbar 
Python :: group by month and year 
Python :: Realtime-yahoo-stock_price 
Python :: create view django not saving image 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =