Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract domain from email address with Pandas

I have no idea to extract domain part from email address with pandas. In case if it is '[email protected]' I would like to get 'gmail.com'.

Please give me an idea.

like image 381
tajihiro Avatar asked Dec 07 '22 13:12

tajihiro


2 Answers

I believe you need split and select second value of lists by indexing:

df = pd.DataFrame({'email':['[email protected]','[email protected]']})

df['domain'] = df['email'].str.split('@').str[1]
#faster solution if no NaNs values 
#df['domain'] = [x.split('@')[1] for x in df['email']]
print (df)
           email     domain
0  [email protected]  gmail.com
1   [email protected]  yahoo.com
like image 148
jezrael Avatar answered Dec 10 '22 03:12

jezrael


This can also be done using lambda function.

df = pd.DataFrame({'email':['[email protected]','[email protected]', '[email protected]']})

df['domain'] = df['email'].apply(lambda x: x.split('@')[1])
like image 44
Akhilesh L. Avatar answered Dec 10 '22 02:12

Akhilesh L.