Extracting from Parenthesis in Pandas

Viewed 63

Can you advise How I can extract something in parenthesis in pandas.

I have a column named Origin with Countries. But I have certain data in parenthesis, I want to get that data and replace it in my original data frame.

Any advise. This is my code so far.

df['Nationality'] = df["Origin"].str.split("(") 

Thanks

2 Answers

Use str.extract with a pattern:

pat = '\((.*)\)'

df['Nationality'] = df["Origin"].str.extract(pat)[0]

For details, see this link.

You can use str.replace() and specify a regular expression pattern:

df['Nationality'] = df["Origin"].str.replace(r'\(|\)', '')

This would replace the parentheses in the Origin.

If you have other data in the column as well, you could extend the pattern to something like this:

df['Nationality'] = df["Origin"].str.replace(r'.*\(|\).*', '')

This would replace everything before and after the parentheses, and remove the parentheses as well.

Related