Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am printing my list of usernames using the below code:

df1 = pd.DataFrame({'Username':commentor_list})

df1.assign(Username=df1.Username.str.split(",")).explode('Username')

However, some usernames output with the extra info like the following:

Alice (10 videos / 58 subscribers)

Bob

Charles (20 videos / 28 subscribers)

Diana

I want to extract my first names to get

Alice 

Bob

Charles 

Diana

I've tried using a code below, as well as the '[\S]' and '[\w] ' but nothing works. I get my error 'pattern contains no capture groups'

df1['Username'] = df1['Username'].str.extract('[?:\S]')

print(df1)

1 Answer

0 votes
by (36.8k points)
edited by

There is no need to explode, I will use the expand=True with the .str.split:

df1['Username'] = df1['Username'].str.split(" ",expand=True)[0]

Want to be a Data Science expert? Come and join this Data Science Courses

Browse Categories

...