Why doesn't this code work?
loan_data['emp_length'].str.replace('+ years', '')
Why do I have to place a forward slash in front of the plus symbol for it to work?
The + symbol is a special character in regular expressions, which means “one or more” of the preceding character. The replace method of pandas Series uses regular expressions by default to match the pattern you want to replace.
To use the + symbol as a literal character instead of its special meaning in regular expressions, you need to escape it by placing a backslash (\) in front of it. So, to replace the string "10+ years" with an empty string, you would use the following code:
loan_data['emp_length'] = loan_data['emp_length'].str.replace('\+ years', '')
Copy
This tells the replace method to treat the + symbol as a literal character instead of its special meaning in regular expressions.