You are mistaken if you do not apply it for each character, you apply it for each word and ord errors, since it takes one character, you need:
df['DB_user'] = df["DB_user"].apply(lambda x: ''.join([" " if ord(i) < 32 or ord(i) > 126 else i for i in x]))
You can also simplify the connection using chain comparison:
''.join([i if 32 < ord(i) < 126 else " " for i in x])
You can also use string.printable to filter characters:
from string import printable st = set(printable) df["DB_user"] = df["DB_user"].apply(lambda x: ''.join([" " if i not in st else i for i in x]))
The fastest way is to translate:
from string import maketrans del_chars = " ".join(chr(i) for i in range(32) + range(127, 256)) trans = maketrans(t, " "*len(del_chars)) df['DB_user'] = df["DB_user"].apply(lambda s: s.translate(trans))
Interestingly, this is faster than:
df['DB_user'] = df["DB_user"].str.translate(trans)
source share