python


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

Related Links

How to do a reverse operation to tf.nn.embedding_lookup?
django complex query for interdependant tables
Is there a more efficient and elegant way to filter pandas index by date?
How do I temporarily use Python 3.6 in terminal Mac?
Difference between pre-trained word embedding and training word embedding in keras
AttributeError: module 'xlwings' has no attribute 'save'
Images broadcast over UDP socket (Python)
Index class as list and as dictionary
Python “will the arrow fly straight program”
Processing an eventlog with Pandas - find next match in DataFrame
Intellij python plugin debugging the script copied under the target directory
Create a list with repeated values with list comprehension
Is LASSO regression implemented in Statsmodels?
There is a duplicate line showed when calling a __init__ in Python script
trouble with mousewheel + scrollbars in tkinter
Python Cutting a string on a certain point

Categories

HOME
batch-processing
redis
elixir
openssl
vb6
filterrific
textwatcher
nuxeo
facebook-graph-api
boost-thread
translation
agile
json-ld
survival-analysis
is-empty
javamail
orientation
checksum
sequelize.js
requirejs
goutte
constructor
data-synchronization
cs-cart
maxima
datagrip
uicollectionview
alfresco-share
arraylist
linq-to-sql
uiautomator
rails-activerecord
deb
fop
servicemix
bcrypt
badge
katharsis
dd-wrt
referenceerror
encase
oracle-xml-db
polymorphism
shapes
mapguide
xamarin.uitest
readfile
topic-modeling
linq-to-entities
tango
spring-bean
stress-testing
tuleap
dimple.js
appstore-approval
jgraph
rollback
decompress
slot
ticker
crash-reports
android-bitmap
scd
nuget-server
pagefile
in-memory-database
pycurl
vst
mongoose-populate
wcf-ria-services
multifile-uploader
soda
anti-cheat
sun-codemodel
fill
portfolio
loopj
vips
twython
xaml-designer
es2015
jcr-sql2
tilestache
cloudpebble
candidate-key
ember-addon
config-files
jquery-tabs
sslexception
robospice
vertical-rhythm
html-form-post
nuspec
resgen
device-emulation
xml-libxml
pitch
web-safe-fonts
icefaces-3
parameterization
responsetext
point-sprites
viewdidload
nssavepanel
path-manipulation
galaxy-tab
goliath
collect
gethashcode
squeel
drawtext
hibernate3-maven-plugin
revert
grooveshark
savestate
microsoft.ink
soft-keyboard
winsnmp
method-signature

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html