Reputation: 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
Upvotes: 3
Views: 310
Reputation: 5237
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.
Upvotes: 1
Reputation: 150785
Use str.extract
with a pattern:
pat = '\((.*)\)'
df['Nationality'] = df["Origin"].str.extract(pat)[0]
For details, see this link.
Upvotes: 2