Skip to content
Advertisement

Replace special characters in pandas dataframe from a string of special characters

I have created a pandas dataframe called df using this code:

import numpy as np import pandas as pd

JavaScript

The dataframe looks like this:

JavaScript

The columns contain some special characters (/ and @) that I need to replace with a blank space.

Now, I have a list of special characters:

listOfSpecialChars = ‘¬`!”£$£#/,.+*><@|”‘

How can I replace any of the special characters listed in listOfSpecialChars with a blank space, any time I encounter them at any point in a dataframe, for any columns? At the moment I am dealing with a 100K-record dataframe with 560 columns, so I can’t write a piece of code for each variable.

Advertisement

Answer

You can use apply with str.replace:

JavaScript

Alternatively, stack/unstack:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement