re.sub erroring with "Expected string or bytes-like object"

As you stated in the comments, some of the values appeared to be floats, not strings. You will need to change it to strings before passing it to re.sub. The simplest way is to change location to str(location) when using re.sub. It wouldn't hurt to do it anyways even if it's already a str.

letters_only = re.sub("[^a-zA-Z]",  # Search for all non-letters
                          " ",          # Replace all non-letters with spaces
                          str(location))

The simplest solution is to apply Python str function to the column you are trying to loop through.

If you are using pandas, this can be implemented as:

dataframe['column_name']=dataframe['column_name'].apply(str)

I had the same problem. And it's very interesting that every time I did something, the problem was not solved until I realized that there are two special characters in the string.

For example, for me, the text has two characters:

‎ (Left-to-Right Mark) and ‌ (Zero-width non-joiner)

The solution for me was to delete these two characters and the problem was solved.

import re
mystring = "‎Some Time W‌e"
mystring  = re.sub(r"‎", "", mystring)
mystring  = re.sub(r"‌", "", mystring)

I hope this has helped someone who has a problem like me.