qua*_*ant 3 python python-3.x pandas
我有以下数据框:
import pandas as pd
df = pd.DataFrame({'col':['text https://random.website1.com text', 'text https://random.website2.com']})
Run Code Online (Sandbox Code Playgroud)
我想删除此专栏中的所有链接。
有任何想法吗 ?
使用带有分割和测试 url 的列表理解,最后按空格连接值:
from urllib.parse import urlparse
#/sf/answers/3671918071/
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
Run Code Online (Sandbox Code Playgroud)