4

My question is how I can use the re to replace strings that included in a dataframe:

when I use the re.sub(), it gives me an error:

p = re.compile('New')
p.sub('old', df['Col1'])

Also, I tried using the for loop but the out put was unexpected and displaying the value of the first row in all the other rows:

for i in df['Col1']:
    p.sub('old', i)
    print(i)

I'm sure that I'm missing something.

1 Answer 1

3

I think you can use str.replace, which works also with regex:

df = pd.DataFrame({'Col1':['sss old','dd','old']})
print (df)
      Col1
0  sss old
1       dd
2      old

df.Col1 = df.Col1.str.replace('old','new')
print (df)
      Col1
0  sss new
1       dd
2      new
Sign up to request clarification or add additional context in comments.

2 Comments

One more question : when i want to use re with the str.startswith. it returns all values false.i.e : df.col1.str.startswith('(N|n)ew'), am i missing something?
str.startswith doesnt work with regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.