Search code examples
pythonpandastext-mining

String replacements in Pandas


I need to replace words starting with www. with the rest of the link. For example:

www.stackoverflow.com 

with

stackoverflow.com

I am using pandas. The column that contains the links is called COL1. I have 1000 rows. I have tried with

df.loc[df['COL1'].str.startswith('www.', na=False), 'NEW_COL'] 

but I do not know what I should replace with in order to take the rest of the link.

Could you please give me advice on it?


Solution

  • You can use str.replace with regex pattern:

    df['COL1'] = df['COL1'].str.replace('^(www\.)', '')