Home > database >  How to replace symbols from object in dataframe
How to replace symbols from object in dataframe

Time:04-08

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.

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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)
  • Related