如何从熊猫中的字符串列中删除 HTTPS 链接



我有以下数据帧:

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

我想从此列中删除所有链接。

有什么想法吗?

将列表推导式与拆分和测试 url 一起使用,最后按空格连接值:

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

使用正则表达式。

前任:

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 

最新更新