Skip to main content
The col() function in Spark is used to reference a column in a DataFrame. It is part of the pyspark.sql.functions module and is commonly used in DataFrame transformations, such as filtering, sorting, and aggregations. The col() function allows you to refer to columns dynamically and is particularly useful when working with complex expressions or when column names are stored in variables.

1. Syntax

PySpark:

2. Parameters

  • column_name: The name of the column to reference (as a string).

3. Return Type

  • Returns a Column object that represents the specified column.

4. Examples

Example 1: Referencing a Column in a Filter Operation

PySpark:
Spark SQL:
Output:

Example 2: Referencing a Column in a Select Operation

PySpark:
Spark SQL:
Output:

Example 3: Using col() in an Expression

PySpark:
Spark SQL:
Output:

Example 4: Using col() with Aliases

PySpark:
Spark SQL:
Output:

Example 5: Using col() in Aggregations

PySpark:
Spark SQL:
Output:

Example 6: Using col() with Conditional Logic

PySpark:
Spark SQL:
Output:

Example 7: Using col() with String Functions

PySpark:
Spark SQL:
Output:

Example 8: Using col() with Mathematical Operations

PySpark:
Spark SQL:
Output:

5. Common Use Cases

  • Referencing columns in DataFrame transformations (e.g., filter(), select(), withColumn()).
  • Building complex expressions for data transformations.
  • Dynamically referencing columns when column names are stored in variables.

6. Performance Considerations

  • Using col() is a metadata operation and does not involve data movement, making it very efficient.
  • Combine col() with other functions (e.g., sum(), avg()) for advanced transformations.

7. Key Takeaways

  1. The col() function is used to reference a column in a DataFrame.
  2. It allows you to dynamically refer to columns and use them in expressions, transformations, and aggregations.
  3. Using col() is lightweight and does not impact performance.
  4. In Spark SQL, columns are referenced directly by name.
  5. Works efficiently on large datasets.