How to delete words between multiple parenthesis in pandas?

3694 views python
6

I want to delete parenthesis and the words between them in pandas dataframe. So far I have searched and found this to be very helpful-

df['Column'].str.replace(r"\(.*\)","")

But this converts the following string-

Lemon ( 20 % ... ), orange (...) and apple (...) done.

(... contain other letters)

to

Lemon done.

However, I want the output to be-

Lemon, orange and apple done.

answered question

1 Answer

7

Use:

df['Column'].str.replace(r"\((.*?)\)","")

Output

Lemon , orange  and apple  done.

Explanation

  1. \( matches the character ( literally (case sensitive)
  2. .*?matches any character (except for line terminators)
  3. \) matches the character ) literally (case sensitive)

posted this

Have an answer?

JD

Please login first before posting an answer.