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 ?
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 ?
 
    
    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
 
    
    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 
