How to replace a pattern in a string?
Hi I am trying to replace all the expressions containing 'www...' and 'http://..' with just 'URL'. I tried this but I am getting this error. TypeError: expected string or buffer My code is: df['text_1'] = re.sub('((www\.[^\s]+)|(https?://[^\s]+))','URL',df['text']) df[text] contains tweets, so I want to keep only the text in there. I am in Python 2 Thanks.
Assuming df is a pandas DataFrame, don't use re.sub. Use pandas.DataFrame.replace instead: df['text_1'] = df['text'].replace('((www\.[^\s]+)|(https?://[^\s]+))', 'URL', regex=True) This will generate a new column text_1 with all values of text replaced according to your regular expression.
It sounds like you're getting that error because you're not supplying a string or buffer as the third argument to re.sub. >>> re.sub('\W', 'REPLACED', 'this is my text') 'thisREPLACEDisREPLACEDmyREPLACEDtext' >>> re.sub('\W', 'REPLACED', None) Traceback (most recent call last): ... TypeError: expected string or buffer Ensure that df['text'] contains a proper string before you try using it for re.sub
NLTK and Stanford parsing
How can I apply numerical gradient in Tensorflow?
How can I generate categorical synthetic samples with imblearn and SMOTE?
Writing a function to initialize classes with uncertain input?
Batch Rendering file from a python script without openeing Maya
Python hex to binary fail
Post and redirect to cross domain URL
Use Unicode text with seaborn
If Loop - Index out of range
Making a Turtle Object Look Look Further for Another Object
Facebook Bot sending reply multiple times
DNS query forwarding if local DNS is resolving
Loading flask configuration using from_object
Python OrderedDict with lambda [duplicate]
TypeError: unorderable types: dict() > dict()
How to install python sipconfig module on win7