2

I have the following dataframe:

import pandas as pd
df = pd.DataFrame({'col':['text https://random.website1.com text', 'text https://random.website2.com']})

I would like to remove all the links from this column.

Any ideas ?

quant
  • 3,328
  • 1
  • 21
  • 49

2 Answers2

4

Use list comprehension with split and test url, last join values by space:

from urllib.parse import urlparse
#https://stackoverflow.com/a/52455972
def is_url(url):
  try:
    result = urlparse(url)
    return all([result.scheme, result.netloc])
  except ValueError:
    return False

df['new'] = [' '.join(y for y in x.split() if not is_url(y)) for x in df['col']]
print (df)
                                     col        new
0  text https://random.website1.com text  text text
1       text https://random.website2.com       text
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090
1

Using regex.

Ex:

import pandas as pd
df = pd.DataFrame({'col':['text https://random.website1.com text', 'text https://random.website2.com']})
#Ref https://stackoverflow.com/questions/10475027/extracting-url-link-using-regular-expression-re-string-matching-python
df["col_new"] = df["col"].str.replace(r'https?://[^\s<>"]+|www\.[^\s<>"]+', "")
print(df)

                                     col     col_new
0  text https://random.website1.com text  text  text
1       text https://random.website2.com       text 
Rakesh
  • 78,594
  • 17
  • 67
  • 103