Skip to main content
 首页 » 编程设计

python之从 python panda 数据框中的大量文本中逐行删除 URL

2025年05月04日16luoye11

我已经将数据插入到 pandas 数据框中。像图片建议的那样 如您所见,有些行包含 url 链接,我想删除所有 url 链接并将它们替换为“”(只是删除它)如您所见,第 4 行有一个 url,还有其他行也有 url。我想遍历 status_message 列中的所有行,找到任何 url 并将其删除。我一直在看这个How to remove any URL within a string in Python但我不确定如何在数据框上使用它。所以第 4 行现在应该投票支持劳工登记。

请您参考如下方法:

您可以使用 str.replace使用 case=False 参数:

df = pd.DataFrame({'status_message':['a s sd Www.labour.com', 
                                    'httP://lab.net dud ff a', 
                                     'a ss HTTPS://dd.com ur o']}) 
print (df) 
             status_message 
0     a s sd Www.labour.com 
1   httP://lab.net dud ff a 
2  a ss HTTPS://dd.com ur o 
 
df['status_message'] = df['status_message'].str.replace('http\S+|www.\S+', '', case=False) 
print (df) 
  status_message 
0        a s sd  
1       dud ff a 
2     a ss  ur o