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
How do I make pip install binaries to a writable path?
TensorFlow: generating a random constant
How can I organize my functions to get something like class.draw.line() instead of class.drawLine
Traceback (most recent call last): (works in trinket.io but not 3.4/2.7.11)
python : Mode of a List
How to send and receive data from flask?
Python HTTPS Login to account to scrape data, is this bad practice?
Celery, redis and ConnectionPool
Numpy Choose Elements from 2 arrays
Selenium Python iterate over a table of rows it is stopping at the first row
How to add a variable of a method of a class in aonther program in python?
Django : Getting error while removing django.contrib.sites from INSTALLED_APPS
Can I scrape a html page which is local machine using scrapy?
how to get request HTTP headers in soaplib views file?
Hive transform query runs slow with ORC file
What if I *really* need to escape quotes for an SQL script?