Home > Software design >  I need a Regex Expression for searching title
I need a Regex Expression for searching title

Time:11-08

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)]

CodePudding user response:

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
  • Related