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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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
Method 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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0