Soor ka bacha
Soor ka bacha

Reputation: 87

Remove a URL row by row from a large set of text in python panda dataframe

i have inserted data into pandas dataframe. like the picture suggest as you can see there are some rows that contain url links i want to remove all the url links and replace them with " " (nothing just wiping it ) dataframe as you can see row 4 has a url there are other rows too that have url. i want to go through all the rows in the status_message column find any url and remove them. i've been looking at this How to remove any URL within a string in Python but am not sure how to use to it on the dataframe. so row 4 should like vote for labour register now.

Upvotes: 2

Views: 7265

Answers (4)

yasir khatri
yasir khatri

Reputation: 101

df.status_message = df.status_message.str.replace("www.", "")

Upvotes: 0

Gayatri
Gayatri

Reputation: 2253

I think you could do something simple as

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')))

as long as the urls start with "www."

Upvotes: 0

jezrael
jezrael

Reputation: 863801

You can use str.replace with case=False parameter:

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

Upvotes: 8

Bharath M Shetty
Bharath M Shetty

Reputation: 30605

You can use .replace() with regex to do that i.e

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)

Output :

                           A
0  Nice to meet you amazing
1                       Wow 
2                   Amazing

Upvotes: 2

Related Questions