pyspark.sql.tvf.TableValuedFunction.stack#

TableValuedFunction.stack(n, *fields)[source]#

Separates col1, …, colk into n rows. Uses column names col0, col1, etc. by default unless specified otherwise.

Added in version 4.0.0.

Parameters:
nColumn

the number of rows to separate

fieldsColumn

input elements to be separated

Returns:
DataFrame

Examples

>>> import pyspark.sql.functions as sf
>>> spark.tvf.stack(sf.lit(2), sf.lit(1), sf.lit(2), sf.lit(3)).show()
+----+----+
|col0|col1|
+----+----+
|   1|   2|
|   3|NULL|
+----+----+