I have following data
df.show
+---------+--------------------+------------+
| name| age| tokens| tokensCount|
+---------+----+---------------+------------+
| Alice| 29| [A,B,C]| 3|
| Bob| 28| [A,B,C,D]| 4|
| Charlie| 29| [A,B,C,D,E]| 5|
+---------+----+---------------+------------+
I transform data with following command
val newDF = df.select(($"name") +: (0 until 4).map(i => ($"tokens")(i).alias(s"token$i")): _*).show
+---------+-------+-------+-------+-------+
| name| token0| token1| token2| token3|
+---------+-------+-------+-------+-------+
| Alice| A| B| C| null|
| Bob| A| B| C| D|
| Charlie| A| B| C| D|
+---------+-------+-------+-------+-------+
I want to give tokensCount
instead of static value 4
at (0 until 4)
I tried a few things like $"tokensCount"
and size($"tokens")
, but could not get through.
Can anyone suggest how to loop or map according to the size of array or count of array ?
Many thanks