'Using regex sub on a df.column with apply in pandas df

I have a df as such:

data= [{'Employees at store': 18, 'store': 'Mikes&Carls P@rlor', 'hair cut inch':'$3'},
        {'Employees at store': 5, 'store': 'Over-Top', 'hair cut inch': '$9'}]

df = pd.DataFrame(data)
df

& have

df=df.apply(lambda x: x.astype(str).str.lower().str.replace(' ','_')
                      if isinstance(x, object)
                      else x)

working for repalacing spaces with underscores. I know that you can link these per How to replace multiple substrings of a string? .

And I also know that the link the exact string, not a subpart of it having tried:

df=df.apply(lambda x: x.astype(str).str.lower().str.
            replace(' ','_').str.
            replace('&','and').str.
            replace('@','a') if isinstance(x, object) else x)

I think I have to use re.sub and do something like this re.sub('[^a-zA-Z0-9_ \n\.]', '', my_str) and can't figure out how to build it into my apply(lambda...) function.



Sources

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

Source: Stack Overflow

Solution Source