Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I need a Regex Expression for searching title

In a dataset, I need to find how many instances of "mr.","mrs." and "miss." there are. For example:

Obama, Mr. Barack Hussein
Obama, Mrs. Michelle LaVaughn Robinson
Biden, Mr. Joe
Portman, Miss. Natalie

In this expression, it must search after "," and must get "Mr." exactly. In my search code, it also counts "mrs" instead of only "Mr.".

Mr = df.loc[df['Name'].str.contains('Mr.', case=False)]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a regex. The titles are distinctive enough that they should not appear anywhere else.

df['Name'].str.extract('(Mr\.|Mrs\.|Miss\.)').value_counts()

output:

Mr.      2
Miss.    1
Mrs.     1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading