Skip to main content

TableValuedFunction.stack

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

Syntax

Python
spark.tvf.stack(n, *fields)

Parameters

Parameter

Type

Description

n

pyspark.sql.Column

The number of rows to separate.

fields

pyspark.sql.Column

Input elements to be separated.

Returns

pyspark.sql.DataFrame: A DataFrame with the stacked rows.

Examples

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