python - 如何从 pandas 的字符串列中删除 https 链接

标签 python python-3.x pandas

我有以下数据框:

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

关于python - 如何从 pandas 的字符串列中删除 https 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56358888/

相关文章:

python - 本地主机服务器拒绝连接

python - 如何添加一条最适合散点图的线

python - 如何对 Pandas DF 列中的值进行排序并删除重复项

python - 如何在 Pandas 中拥有一列完整的列表

python - 使用 pd.read_csv 时跳过日期不正确的行

python - Django - 如何通过用户先前选择的选项填充表单中的 manytomany 字段

python - 如果列表中的项目与字典中子元素的列表中的项目匹配

python-3.x - 如何使用 pygame 和 pyopengl 正确添加灯光以使对象获得更好的 View

python - 列表值的字典理解

python - Python 请求的 SSL 证书问题