3

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
  • 4,062
  • 5
  • 29
  • 70

2 Answers2

5

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
  • 822,522
  • 95
  • 1,334
  • 1,252
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
  • 81,458
  • 17
  • 76
  • 113