Home > Net >  How to use urlparse in python pandas
How to use urlparse in python pandas

Time:04-28

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
http://robotichairrx.com/
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().

CodePudding user response:

A simple solution would be to use Series.apply

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