Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to replace parentheses from a string in pandas dataframe

Lets say i have a dataframe like this:

       Col1
0     Test1_2345)
1     Test2_(123
2     Test3_567)_rt
3     Test5_874)

How can I replace "(" and ")" from the strings in Col1 and have a dataframe like this:

       Col1
0     Test1_2345
1     Test2_123
2     Test3_567_rt
3     Test5_874

I tried this one but it is not working:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df['Col1'] = df['Col1'].replace("(","",regex=True)
df['Col1'] = df['Col1'].replace(")","",regex=True)

>Solution :

Replace everything other than \w . \w Matches alphanumeric characters and the underscore, _.

df1['Col1'] = df1['Col1'].str.replace('[^\w]','', regex=True)

0      Test1_2345
1       Test2_123
2    Test3_567_rt
3       Test5_874
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading