2

I have a dataframe to store cars related information and I am trying to replace certain key "[Pink & Blue]" to "[Pink&Blue]" to remove the space in between using regex but my code fails to do so :

Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus'],
        'Price': [22000,np.nan,35000] ,
        'Product Summary' : ['color is [Pink & Blue] size is 14' ,'color is [Pink & Yellow] size is 10','color is [Red & Black] size is 11']
    }
df = DataFrame(Cars,columns= ['Brand', 'Price', 'Product Summary'])
df['Product Summary'] = df['Product Summary'].replace(r'\b & ',"&")

1 Answer 1

1

Use Series.str.replace accessor

df['Product Summary'] = df['Product Summary'].str.replace(' & ', '&')

OR In your code, use regex=True

df['Product Summary'] = df['Product Summary'].replace(r'\b & ', '&', regex=True)
Sign up to request clarification or add additional context in comments.

Comments

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.