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

Turn pandas dataframe list into boolean column
How to handle concatenate with empty matrix
python django translation .po and .mo file not translating the files
jupyter not using version set by pyenv
Generalize print+format for a variable number of inputs
What are the differences in these two codes? [closed]
What is the correct way to change image channel ordering between channels first and channels last?
Python to capture screenshot of big file
How can I compare the difference between digits of two numbers?
How to find specific list among other lists
Use variable inside open and also use find and replace in python [on hold]
Can robot framework keyword can be executed in python console?
OpenMDAO/ScipyOptimizer “UnboundLocalError: local variable 'f_new' referenced before assignment”
From C to Python passage by pointer and modification of values [closed]
Is Session.run(fetches) guaranteed to execute its “fetches” arguments in-order?
Return a board read from open file board_file. The board file will contain one row of the board per line

Categories

HOME
paypal
dotnetrdf
firebase-app-indexing
paramiko
boost-thread
cpu-architecture
jbehave
reportportal
d
windows-store-apps
travis-ci
specflow
webdav
hyperledger-fabric
docker-windows
i2c
eval
unity2d
lotus-notes
cs-cart
cosmicmind
pdo
zope
saiku
movies
jconsole
rails-activerecord
countif
emv
widevine
maximo
dss
nesc
fileinfo
bitcoin-testnet
superscript
devforce
hibernate-cache
lmdb
rowcount
strophe
apache-cloudstack
construct-2
superpowered
lego
nodeclipse
mms
vmd
visualstudio.testtools
linq-to-entities
g1gc
ttcn
lirc
azure-availability-set
taskmanager
jags
web-deployment-project
tiddlywiki
udev
pagefile
adler32
gradle-script-kotlin
tomee
packagemaker
uistackview
debugdiag
pydio
radians
scriptlet
picking
interactive-brokers
android-recyclerview
cctray
function-fitting
ffserver
gray-code
jazz
ember-cli-addons
java-collections-api
codeigniter-a3m
unison
zipalign
flash-cc
windows-mobile-6
modeshape
stagefright
client-library
xml-libxml
icsharpcode
bulbs
moq-3
vim-powerline
javascriptserializer
mpmovieplayer
die
visual-web-gui
jquery-selectbox
selectmanycheckbox
newsstand-kit
squeel
svn-hooks
asp.net-mvc-controller
economics

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile