Skip to content
Advertisement

How to split string and get only one word in python [closed]

I have a string similar like this:

HELLO TEST PACKAGE PARIS1 PROJECT

I got this string from selecting row and column:

project_name = df[col_pro].values[row_pro]

and I want to get the “PARIS” only to be in a new column which is ‘location_id’

I tried to split them, but it was hard for me to get the “PARIS” only.

How can I do this with python? Thank you

Advertisement

Answer

I would use Named groups. Say you have df;

                               text
0  HELLO TEST PACKAGE PARIS1 PROJECT

Using Named Groups

df.text.str.extract(r'(?P<location_id>PARIS)')
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement