2017-07-30 39 views
0

我已經將數據插入到熊貓數據框中。像圖片建議 ,你可以看到有一些行包含url鏈接,我想刪除所有的url鏈接,並用「」替換它們(沒有東西只是擦除它)dataframe,你可以看到第4行有一個URL有其他也有網址的行。我想通過status_message列中的所有行查找任何網址並將其刪除。我一直在尋找這How to remove any URL within a string in Python,但我不知道如何在數據幀上使用它。所以第4行現在應該喜歡投票。從python熊貓數據框中的大量文本中逐行刪除一個URL

回答

0

我認爲你可以做一些簡單的

for index,row in data.iterrows(): 
    desc = row['status_message'].lower().split() 
    print ' '.join(word for word in desc if not word.startswith(('www.','http'))) 

只要網址開頭爲「WWW」。

+0

某些URL以http: ... –

+0

請upvote,如果它回答你的問題 – Gayatri

0

您可以使用.replace()用正則表達式來做到這一點,即

df = pd.DataFrame({'A':['Nice to meet you www.xy.com amazing','Wow https://www.goal.com','Amazing http://Goooooo.com']}) 
df['A'] = df['A'].replace(r'http\S+', '', regex=True).replace(r'www\S+', '', regex=True) 

輸出:

 
          A 
0 Nice to meet you amazing 
1      Wow 
2     Amazing 
4

您可以使用str.replacecase=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 
+1

是的,非常相似,只有一個區別 - case = False,用於區分大小寫。 – jezrael

+1

加上一個'case = False' – Dark

相關問題