1

I have a bunch of words I want to replace to "". I put them in a list:

noise = [a,b,c,...]

Example dataset:

column1
I want a car

Becomes:

column1
I wnt cr

How can I use the list to iterate over in replace function. This is my current code :

df[columnA] = df[columnA].str.replace((x for x in noise), "")

But I keep getting error.. I have to apply the same list to several other columns as well. So I'm looking for a shorter way to do this and avoiding apply function.

Error:

TypeError: object of type 'generator' has no len()

If add len(noise), I'll get this error:

TypeError: 'int' object is not iterable

xixi
  • 59
  • 8

1 Answers1

1

This should work:df[columnA][i if i not in noise else ""]

peter123
  • 181
  • 10
  • Will this apply to whole string or just substring? In my case, I'm looking for substring replacement – xixi May 06 '20 at 08:41