How to split names in Pandas dataframe using regex. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to split names in Pandas dataframe using regex.

The name is in the form Title. First_Name Middle_Name Last_Name in a dataframe

17th Feb 2019, 7:57 AM
Amer Jyothi
Amer Jyothi - avatar
2 Answers
+ 3
I got those regex working now :) with the help of the non-capturing groups, like: (?:regex) df['title'] = df['fullname'].str.extract('(\w+\.) ') df['firstname'] = df['fullname'].str.extract('(?:\w+\.\s)?(\w+)') df['middlename'] = df['fullname'].str.extract('(?:\w+\.\s)?(?:\w+\s)(\w+)(?:\s[\w-]+)
#x27;) df['lastname'] = df['fullname'].str.extract('([\w-]+)
#x27;)
21st Feb 2019, 8:37 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Hi I was interested to solve this but it's not 100% complete... only the middle name is missing :P I haven't come up with a viable way for that one yet... I'll think about it later. https://code.sololearn.com/cBYTmzVN4yaH/#py EDIT: code updated!
20th Feb 2019, 9:56 PM
Tibor Santa
Tibor Santa - avatar