0

Why these two are not the same:

df['REFERENCE'] =df['REFERENCE'].astype(str).str.strip().apply(lambda x:x.replace('(','')).apply(lambda x:x.replace(')',''))

df['REFERENCE'] =df['REFERENCE'].astype(str).str.strip().replace('(','')).replace(')',''))

The second one does not give error, but does not substitute '(' by ''.

1 Answer 1

2

Try:

df['REFERENCE'].astype(str).str.strip().str.replace('(','')).str.replace(')',''))

a Pandas Series (column) has both a Series.replace method and a Series.str.replace replace method. The string accessor .str is necessary to use the string based methods which is what you're expecting.

*Series.replace("foo", "bar") looks in each cell of the Series, and if the value is equal to "foo" it is replaced with "bar". So for this method to work, a value in the Series must be exactly equal to "foo", nothing more, nothing less.

*Series.str.replace("foo", "bar") looks in each cell of the Series, and checks replaces ANY PART of the value that is "foo" with "bar". So in this case, the values inside the Series can be strings that simply contain the string "foo" and it will replace the portion of that string with "bar"

Sign up to request clarification or add additional context in comments.

1 Comment

Sooooo good !!!! thanks. Real KEY ISSUE --> accesing the string of the cells!!!

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.