'replace alphanumeric values in a column dataframe

I have a big data frame that looks like this:

a, b, c
4f5t-4656, x, y
3jsu-56hj, x, y
gfhdu670-9, x, y
fgfj-6fhf, x, y
ELE, x, y
ELE, x, y

My goal is to replace all the alphanumeric values in column a by the the letters 'LCD'. I have tried:

df['a']=df['a'].replace([a-z0-9-], 'LCD', regex=True)

but I am getting the "SyntaxError: invalid syntax"

What's the problem with the code? can anyone help?



Solution 1:[1]

My bet is that you don't want to replace each matching character by LCD, but the whole series of characters, thus you probably want to add a + quantifier in your regex (in addition to the missing quotes that give you the SyntaxError):

df['a'] = df['a'].replace('[a-z0-9-]+', 'LCD', regex=True)

Output:

     a  b  c
0  LCD  x  y
1  LCD  x  y
2  LCD  x  y
3  LCD  x  y
4  ELE  x  y
5  ELE  x  y

Solution 2:[2]

You simply need to wrap this expression in quotes

df['a'].replace(r'[a-z0-9-]', 'LCD', regex=True)

I think that should work

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 mozway
Solution 2 Daniel Weigel