str.replace method not working within a user defined function when map to pandas series; however, it works on a normal function call. Why?
RE_EMOJI = re.compile('[\U00010000-\U0010ffff]', flags=re.UNICODE)
def clean_story(x):
x = x.strip() # trims leading and trailing whitespace only
x = x.replace('\r', '').replace('\n', '') # remove all 3 types of line breaks
translator = str.maketrans('','',string.punctuation) # strip punctuation, function
x = x.translate(translator) # strip punctuation
x = RE_EMOJI.sub(r'', x) # remove emojis
x = re.sub(r'http\S+', '', x, flags=re.MULTILINE) # remove url http
x = re.sub(r'\(http\S+', '', x, flags=re.MULTILINE) # remove url (http
x = re.sub(r'@\S+\s', '', x, flags=re.MULTILINE) # remove words starting with @
x = re.sub(' +', ' ', x) # remove duplicated space
x = x.lower() # lower case
return x
x is an example text
x = " Overview \n Using \r Home #; automation: to- control applications \r is cool, but we can further leverage its power to simplify our daily tasks like checking gas, geysers, heaters, AC temperature etc. before leaving home. \n Scope \n The server (Raspberry pi 2 running on Windows 10) is running at home. Family members will stay connected through their smart devices like Mobile phones with Windows Operating systems or Android. The server will be connected to gas, heat and smoke detection sensors. And will push notifications to all the clients (family members), on any changes detected. The server will push notifications to all the clients using Microsoft Azure- Notification Hubs. \n Components and supplies Software and applications Summary \n "
Use this text as an example: calling clean_story(x) results in the proper removal of all "\n"
'overview using home automation to control applications is cool but we can further leverage its power to simplify our daily tasks like checking gas geysers heaters ac temperature etc before leaving home scope the server raspberry pi 2 running on windows 10 is running at home family members will stay connected through their smart devices like mobile phones with windows operating systems or android the server will be connected to gas heat and smoke detection sensors and will push notifications to all the clients family members on any changes detected the server will push notifications to all the clients using microsoft azure notification hubs components and supplies software and applications summary'
However, when placing the same text in a pandas dataframe and map the function to the series containing the text... all the methods seem to work except the str.replace... I'm left with a bunch of "n" (note: same as "\n" because later method removes punctuation)
df_story['clean_story'] = df_story['story'].map(clean_story)
result
'overview n using home automation to control applications is cool but we can further leverage its power to simplify our daily tasks like checking gas geysers heaters ac temperature etc before leaving home n scope n the server raspberry pi 2 running on windows 10 is running at home family members will stay connected through their smart devices like mobile phones with windows operating systems or android the server will be connected to gas heat and smoke detection sensors and will push notifications to all the clients family members on any changes detected the server will push notifications to all the clients using microsoft azure notification hubs n components and supplies software and applications summary n'