Home > database >  replace alphanumeric values in a column dataframe
replace alphanumeric values in a column dataframe

Time:05-01

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?

CodePudding user response:

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

CodePudding user response:

You simply need to wrap this expression in quotes

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

I think that should work

  • Related