pyspark.sql.functions.reverse

pyspark.sql.functions.reverse(col: ColumnOrName) → pyspark.sql.column.Column[source]

Collection function: returns a reversed string or an array with reverse order of elements.

New in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or str

name of column or expression

Returns
Column

array of elements in reverse order.

Examples

>>> df = spark.createDataFrame([('Spark SQL',)], ['data'])
>>> df.select(reverse(df.data).alias('s')).collect()
[Row(s='LQS krapS')]
>>> df = spark.createDataFrame([([2, 1, 3],) ,([1],) ,([],)], ['data'])
>>> df.select(reverse(df.data).alias('r')).collect()
[Row(r=[3, 1, 2]), Row(r=[1]), Row(r=[])]