Here is my code for getting domain name from string:
JavaScript
x
4
1
domain = urlparse('http://www.example.test/foo/bar').netloc
2
print(domain)
3
>>>www.example.test
4
I don’t know how to use this method in pandas. Here is my pandas dataframe:
JavaScript
1
5
1
domain
2
http://www.example.test/foo/bar
3
Robotic Hair Rx
4
http://naturally-yours-hair-co.business.site/
5
I tried this code but didn’t work:
JavaScript
1
2
1
df['domain'] = urlparse(df['domain']).netloc
2
getting this error:
JavaScript
1
2
1
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
2
Advertisement
Answer
A simple solution would be to use Series.apply
JavaScript
1
2
1
df['domain'] = df['domain'].apply(lambda domain: urlparse(domain).netloc)
2