-1

Names which contains honorifics like-

  1. Mr. Evans
  2. Aley Fred,Jr.

I want to remove all prefix and suffix from names specially all different kind of honorifics used in names in pandas.

As an output,I want-

  1. Evans
  2. Aley Fred

I have used some codes but it dosnt work in some cases, I want a very robust code. Is there any way to do that?

Red Vibes
  • 79
  • 8
  • Hi, Welcome to SO. What have you tried? What have you got? Please ensure you include the [Minimum Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Joshua Jan 04 '21 at 06:00
  • @Joshua thans a lot, actually my problem is solved for now and from next time I'll surely include like what I have tried and what I have got and what I need as output, all these things. – Red Vibes Jan 04 '21 at 09:01

1 Answers1

2

You can have a replace for regex that's matching all the prefixes. For example:

>>> pat = r'(Mr|Jr)\.?'

# 'col_name' is the name of the column where your names are.
>>> df['col_name'].replace(pat,'',regex=True)

#If you want your change to be applied inplace just add `inplace`:
>>> df['col_name'].replace(pat,'',regex=True, inplace=True)

Edit

If you want to include other titles you just update the regex

>>> pat=r'(\,|\.|Mrs|Jr|Dr|Mr)'
>>> df

   ID            Name
0   1       Mr. Evans
1   2   Aley Fred,Jr.
2   3  Mrs. Sheen,Jr.

>>> df['Name'].replace(pat,'',regex=True)
0        Evans
1    Aley Fred
2        Sheen
Danail Petrov
  • 1,875
  • 10
  • 12
  • 1
    Thanks Danail,..well I have some doubts, firstly- does it removes suffix as well as prefix both? Secondly- if I get some different honorifics like reverend, Dr, Mrs, Ms then what changes I'll have to make? – Red Vibes Jan 03 '21 at 18:54
  • 1
    It should do both. Checkout my updated answer. Hope that helps & makes sense. – Danail Petrov Jan 03 '21 at 19:07
  • 1
    Yes. I checked...thanks DP. I have some doubt regarding next qstn i.e I have a very complex table in excel file containing merged cells and multiple rows within a column. Now the thing is how can I ask such qstn because if I add screen shot then it would be easier for me but it's not allowed in stckovrflow and secondly GitHub markdown table dosnt provide that feature , it only create a basic table. Can you help me out with this.? – Red Vibes Jan 03 '21 at 19:14
  • 1
    It's not that you're not allowed to post images in SO, but it definitely it is not preferred. Best advice I can give you is to start a new question, make an effort to create a good enough sample of your data & then post it here as table. You mention excel - pandas can read excel, so don't worry to give that a whirl. Once you have your datasheet read by pandas (say `df`) simply post `df.head(5)` and let us know what is what you need and how do you need this output to be looking. That should be a good start. – Danail Petrov Jan 03 '21 at 19:18
  • 1
    Ok DP, I'll surely try to do that. Actually I'm new to SO and I'm learning how to post a good qstn(struggling a bit but I will manage). Thanks a ton to all of you coz you guyz are really very helpful. May God bless you DP. Can you upvote my qstn to improve my reputation, just a small request. – Red Vibes Jan 03 '21 at 19:23
  • Hey, no worries. [Check this one](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Danail Petrov Jan 03 '21 at 19:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226783/discussion-between-danail-petrov-and-red-vibes). – Danail Petrov Jan 03 '21 at 19:38