Skip to content Skip to sidebar Skip to footer

Using Str.split For Pandas Dataframe Values Based On Parentheses Location

Let's say I have the following dataframe series df['Name'] column: Name 'Jerry' 'Adam (and family)' 'Paul and Hellen (and family):\n' 'John and Peter (and family

Solution 1:

Solution with split - is necessary escape ( by \:

df['Name']= df['Name'].str.split("\s+\(").str[0]
print (df)
               Name
0           'Jerry'
1             'Adam
2  'Paul and Hellen
3   'John and Peter

Solution with regex and replace:

df['Name']= df['Name'].str.replace("\s+\(.*$", "")
print (df)
               Name
0           'Jerry'
1             'Adam
2  'Paul and Hellen
3   'John and Peter

\s+\(.*$ means replace from optional whitespace, first ( to the end of string $ to "" - empty string.

Solution 2:

Use regular expression:

>>>import re>>>str = 'Adam (and family)'>>>result = re.sub(r"( \().*$", '', str)>>>print result
Adam

Post a Comment for "Using Str.split For Pandas Dataframe Values Based On Parentheses Location"