Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing URL from a column in Pandas Dataframe

I have a small dataframe and am trying to remove the url from the end of the string in the Links column. I have tried the following code and it works on columns where the url is on its own. The problem is that as soon as there are sentences before the url the code won't remove those urls

Here is the data: https://docs.google.com/spreadsheets/d/10LV8BHgofXKTwG-MqRraj0YWez-1vcwzzTJpRhdWgew/edit?usp=sharing (link to spreadsheet)

import pandas as pd  

df = pd.read_csv('TestData.csv')    

df['Links'] = df['Links'].replace(to_replace=r'^https?:\/\/.*[\r\n]*',value='',regex=True)

df.head()

Thanks!

like image 570
Joe Smith Avatar asked Oct 17 '25 11:10

Joe Smith


2 Answers

Try this:

import re
df['cleanLinks'] = df['Links'].apply(lambda x: re.split('https:\/\/.*', str(x))[0])

Output:

df['cleanLinks']

    cleanLinks
0   random words to see if it works now 
1   more stuff that doesn't mean anything 
2   one last try please work 
like image 77
Vishnu Kunchur Avatar answered Oct 19 '25 02:10

Vishnu Kunchur


Try a cleaner regex:

df['example'] = df['example'].replace(r'http\S+', '', regex=True).replace(r'www\S+', '', regex=True)

Before implementing regex in pandas .replace() or anywhere else for that matter you should test the pattern using re.sub() on a single basic string example. When faced with a big problem, break it down into a smaller one.

Additionally we could go with the str.replace method:

df['status_message'] = df['status_message'].str.replace('http\S+|www.\S+', '', case=False)
like image 25
Philip DiSarro Avatar answered Oct 19 '25 02:10

Philip DiSarro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!