I need to get the second last word from a string value.
df = spark.createDataFrame([
["sample text 1 AFTEDGH XX"],
["sample text 2 GDHDH ZZ"],
["sample text 3 JEYHEHH YY"],
["sample text 4 QPRYRT EB"],
["sample text 5 KENBFBF XX"]
]).toDF("line")
+--------+
|word |
+--------+
|AFTEDGH |
|GDHDH |
|JEYHEHH |
|QPRYRT |
|KENBFBF |
+--------+
I tried:
df_new = df.withColumn('word', F.split(F.col('line'), ' ')[-2])
df_new = df.withColumn('word', F.reverse(F.split(F.col('line'), ' '))[-2])
But they return Null