cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Community Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

New to PySpark

Anku_
New Contributor II

Hi all,

I am trying to get the domain from an email field using below expression; but getting an error.

Kindly help.

 

df.select(df.email, substring(df.email,instr(df.email,'@'),length(df.email).alias('domain')))
2 REPLIES 2

Anku_
New Contributor II

Note: I have also tried:

 df.select(df.email, substring(df.email,instr(df.email,'@')+1,length(df.email) - instr(df.email,'@')+1).alias('domain'))

Walter_C
Valued Contributor III
Valued Contributor III

In your case, you want to extract the domain from the email, which starts from the position just after '@'. So, you should add 1 to the position of '@'. Also, the length of the substring should be the difference between the total length of the email and the position of '@'.

Can you try with the below:

from pyspark.sql.functions import instr, length, substring

df.select(df.email, substring(df.email, instr(df.email, '@') + 1, length(df.email) - instr(df.email, '@')).alias('domain'))

This code will create a new column 'domain' that contains the domain of the email.

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!