Removing parenthesis from a string in pandas with str.replace

str.replace uses regex to perform replacements. The parentheses must be escaped to keep them as simple characters:

energy['Country'].str.replace("Bolivia \(Plurinational State of\)","Bolivia")

You can automate escaping like this:

import re
energy['Country'].str.replace(re.escape('Bolivia (Plurinational State of)'),"Bolivia")

This removed all instances of where there were parentheses with words in them:

energy['Country'] = energy['Country'].str.replace(r"\(.*\)","")

energy['Country'] = energy['Country'].str.replace(r"\s+\(.*\)","")

Solution of @python_new_user but solve a white trailing problem mention by @Boud

Tags:

Pandas

Regex