Skip to content
Advertisement

How to use urlparse in python pandas

Here is my code for getting domain name from string:

domain = urlparse('http://www.example.test/foo/bar').netloc
print(domain)
>>>www.example.test

I don’t know how to use this method in pandas. Here is my pandas dataframe:

          domain
http://www.example.test/foo/bar
Robotic Hair Rx
http://naturally-yours-hair-co.business.site/

I tried this code but didn’t work:

df['domain'] = urlparse(df['domain']).netloc

getting this error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Advertisement

Answer

A simple solution would be to use Series.apply

df['domain'] = df['domain'].apply(lambda domain: urlparse(domain).netloc)
Advertisement