我有以下数据框:
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/