Skip to main content
The alias() function in Spark is used to rename a column or an expression in a DataFrame. It is particularly useful when you want to give a more meaningful name to a column, especially after performing transformations or aggregations. The alias() function can be applied to columns, expressions, or even entire DataFrames.

1. Syntax

PySpark:
Spark SQL:

2. Parameters

  • new_name: The new name to assign to the column or expression.

3. Return Type

  • Returns a Column object with the new name.

4. Examples

Example 1: Renaming a Column

PySpark:
Spark SQL:
Output:

Example 2: Renaming an Expression

PySpark:
Spark SQL:
Output:

Example 3: Renaming Multiple Columns

PySpark:
Spark SQL:
Output:

Example 4: Renaming a DataFrame

PySpark:
Spark SQL:
Output:

Example 5: Using alias() with Aggregations

PySpark:
Spark SQL:
Output:

Example 6: Renaming Columns in a Join

PySpark:
Spark SQL:
Output:

Example 7: Renaming Columns in a Nested DataFrame

PySpark:
Spark SQL:
Output:

5. Common Use Cases

  • Renaming columns after transformations or aggregations.
  • Assigning meaningful names to derived columns.
  • Renaming DataFrames for clarity in joins or complex queries.

6. Performance Considerations

  • Using alias() is lightweight and does not involve data movement.
  • It is particularly useful for improving the readability of complex queries.

7. Key Takeaways

  1. The alias() function is used to rename columns, expressions, or DataFrames.
  2. In Spark SQL, similar functionality can be achieved using AS.