pyspark.sql.DataFrame.alias

DataFrame.alias(alias: str) → pyspark.sql.dataframe.DataFrame[source]

Returns a new DataFrame with an alias set.

New in version 1.3.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
aliasstr

an alias name to be set for the DataFrame.

Returns
DataFrame

Aliased DataFrame.

Examples

>>> from pyspark.sql.functions import col, desc
>>> df = spark.createDataFrame(
...     [(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
>>> df_as1 = df.alias("df_as1")
>>> df_as2 = df.alias("df_as2")
>>> joined_df = df_as1.join(df_as2, col("df_as1.name") == col("df_as2.name"), 'inner')
>>> joined_df.select(
...     "df_as1.name", "df_as2.name", "df_as2.age").sort(desc("df_as1.name")).show()
+-----+-----+---+
| name| name|age|
+-----+-----+---+
|  Tom|  Tom| 14|
|  Bob|  Bob| 16|
|Alice|Alice| 23|
+-----+-----+---+