Tugrul Gokce
Tugrul Gokce

Reputation: 300

How can I get rid of the parentheses in the dataframe column?(Python)

I have dataframe like this

enter image description here

I want to remove the parentheses in the Movie_Name column.

Here is a few example

df_Movie["Movie Name"].str.findall(r'\([^()]*\)').sum()

['(500)',
 '(Eksiteu)',
 '(Mumbai Diaries)',
 '(Geukhanjikeob)',
 '(Erkekler Ne İster?)',
 '(The Witch)',
 '(Ji Hun)']

And then ı tried this solution.

import re
df_Movie["Movie Name"] = df_Movie["Movie Name"].str.replace(r'\([^()]*\)', ' ', regex = True)

Here is the output of the solution for one example.

df_Movie.iloc[394, :]
Movie Name      Days of Summer
Year                      2009
IMDB                       7.7
Name: 394, dtype: object

In this case, the values ​​between the parentheses are completely lost. I don't want that.

I want output like this : (500) Days of Summer --> 500 Days of Summer

How can ı solve this ?

Upvotes: 1

Views: 154

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

You can remove all parentheses from a dataframe column using

df_Movie["Movie Name"] = df_Movie["Movie Name"].str.replace(r'[()]+', '', regex=True)

The [()]+ regex pattern matches one or more ( or ) chars.

See the regex demo.

Upvotes: 2

Related Questions