1

I would like to have a code in order to remove special character such as ? from values in a column in a dataframe.

I would like to remove from

()?()()() hello world'

the () and the ?.

However, when I am using the following code:

Community_final_level_2_description = Community_final_level_2_description.replace('()', ' ', regex=True) 

or

Community_final_level_2_description.replace('?', ' ', regex=True)

it is not working and I get an error.

Can someone provide me the proper code ?

Azhar Khan
  • 3,829
  • 11
  • 26
  • 32

2 Answers2

2

(/)/? are special characters for regex. Try to escape them:

df["column"] = df["column"].replace(r"\?|\(\)", "", regex=True)
print(df)

Prints:

         column
0   hello world

Dataframe used:

                  column
0  ()?()()() hello world
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1

If one has a dataframe df that looks like the following

                       A                      B
0  ()?()()() hello world  ()?()()() hello world
1  ()?()()() hello world  ()?()()() hello world
2  ()?()()() hello world  ()?()()() hello world

And one wants to remove the () and ? from every column, one can use pandas.DataFrame.apply with a custom lambda function as follows

df_new = df.apply(lambda x: [i.replace('()', '').replace('?', '') for i in x])

[Out]:

              A             B
0   hello world   hello world
1   hello world   hello world
2   hello world   hello world

Notes:

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83