Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove "http://", "https://", or "www." from string in pandas

New to Python/pandas.

Within a column called "URL", I am trying to replace any URLs that have "http://", "https://", or "www." and just keep everything after it.

For example,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

http://www.jhu.edu
http://www.brown.edu
http://https://www.amherst.edu
The State of the University

Should look like:

jhu.edu
brown.edu
amherst.edu
usc.edu

>Solution :

# example
import pandas as pd
data = {'colA': ['http://www.jhu.edu', 'http://www.brown.edu', 'http://https://www.amherst.edu', 'http://www.usc.edu']}
df = pd.DataFrame(data)

use str.replace with regex

out = df['colA'].str.replace(r'https?://|www\.', '', regex=True)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading