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
Copy C structure to Python List
python error:List index out of range
While loop inside for loop?
Serialize multiple models in a single view
Iterating over a list adding attributes to an object in Python [duplicate]
Fourier Transformation vs Numpy FFT
pandas to_html no value representation
Using Pandas To Find the Number Of Periods Since the Rolling High
List Comprehension For Loop + Ternary Operation For Loop?
Pass variable to multiple templates in pybottle
Next and Before Links for a django paginated query
Python - String of Digits to Integer using Recursion?
Weird type error arising when I add to a database using SQL Alchemy
Python for loop causes system crash
Class object variable = none
Print last <td> in beautiful soup