I want to make a column id1 in ss_df such that if length of id is 13, then take substring from 6th digit to end of digits; else when length of id is 9 take the substring from the 3rd digit to the end.
How do I achive this using pyspark?
ss_df :
| id | id1 |
|---|---|
| 9999945678909 | 45678909 |
| 789065437 | 9065437 |
| 67898095 | 67898095 |
What I tried :
from pyspark.sql.functions import length
if (length(col("id")) == 13):
ss_df = ss_df.withColumn("id1", substring("id",6,15))
if (length(col("id")) == 9):
ss_df = ss_df.withColumn("id1", substring("id",3,15))