'How to replace symbols from object in dataframe

I'm trying to replace symbols from object in python, I used

df_summary.replace('\(|\)!,"-', '', regex=True)

enter image description here

but it didn't change anything.



Solution 1:[1]

The replace function is not in place. This means that your dataframe will be unchanged, and the result is returned as the return value of the replac function. You can the the inplace parameter of replace:

df_summary.replace('\(|\)!,"-', '', regex=True, inplace=True)

Most of the pandas functions are note in place and require if needed either the inplace argument, or the assignement of the result to a new dataframe.

Solution 2:[2]

You can either do

df_summary.replace('\(|\)!,"-', '', regex=True, inplace=True)

or

df_summary = df_summary.replace('\(|\)!,"-', '', regex=True)

When you only do df_summary.replace..., this line returns you a pandas list. You forgot to save it. Please add comments to further assist you

Solution 3:[3]

Apart from addind regex=True or setting the result to df_summary again, your are using a pattern:

`\(|\)!,"-` 

That matches matches either ( OR the string \)!,"-

As you are referring to symbols, and you want to replace all separate chars ( ) ! , " - you can use a repeated character class [()!,"-]+ to replace multiple consecutive matches at once.

df_summary.replace('[()!,"-]+', '', regex=True, inplace=True)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 robinood
Solution 2 sam
Solution 3 The fourth bird