remove first character of a spark string column

Viewed 10265

I wonder as I said in title how to remove first character of a spark string column, for the two following cases:

val myDF1 = Seq(("£14326"),("£1258634"),("£15626"),("£163262")).toDF("A")
val myDF2 = Seq(("a14326"),("c1258634"),("t15626"),("f163262")).toDF("A")
myDF1.show
myDF2.show

+--------+
|       A|
+--------+
|£14326  |
|£1258634|
|£15626  |
|£163262 |
+--------+

+--------+
|     A  |
+--------+
|a14326  |
|c1258634|
|t15626  |
|f163262 |
+--------+

I would like to obtain:

+--------+-------+
|       A|      B|
+--------+-------+
|£14326  |  14326|
|£1258634|1258634|
|£15626  |  15626|
|£163262 | 163262|
+--------+-------+

+--------+-------+
|       A|      B|
+--------+-------+
|a14326  |14326  |
|c1258634|1258634|
|t15626  |15626  |
|f163262 |163262 |
+--------+-------+

Do you have any idea?

1 Answers

You can do something like this.

myDF1.show
+------+
|     A|
+------+
|£14326|
|£12586|
|£15626|
|£16326|
+------+

myDF1.withColumn("B", expr("substring(A, 2, length(A))")).show
+------+-----+
|     A|    B|
+------+-----+
|£14326|14326|
|£12586|12586|
|£15626|15626|
|£16326|16326|
+------+-----+
Related