The sample code you shared should work correctly because customer_id exists in the DataFrame schema. The issue is most likely occurring in one of the intermediate transformations in your actual pipeline.
I have worked with PySpark DataFrames in Databricks, and one thing I would check first is whether customer_id is still available in the exact DataFrame being used when the exception occurs.
Spark resolves column references against the output schema of the current transformation, not the original DataFrame.
For example, consider this sequence:
The original DataFrame contains customer_id and customer_name.
A transformation selects only customer_name.
A later transformation tries to access customer_id.
Spark raises UNRESOLVED_COLUMN because customer_id is no longer present in the current DataFrame.
The column existed initially, but it was removed from the transformation output.
I would investigate this in the following order.
Check the schema immediately before the failing transformation using printSchema().
Print df.columns to confirm the exact column names available at that stage.
Review earlier select(), drop(), withColumnRenamed() and aggregation operations.
If joins are involved, check whether aliases or duplicate column names are causing incorrect references.
Use explain(True) to inspect the logical plan and identify where a column was removed or renamed.
Check the complete exception message, especially the suggested column names.
One important point is that DataFrame aliases do not automatically remove columns. However, referencing a column through the wrong DataFrame or alias after a join can cause resolution problems. Joins can also create ambiguous column references when both inputs contain the same name.
For a complex pipeline, I would break the transformations into smaller named DataFrames and inspect the schema after each major step.
For example, use df_stage1, df_stage2 and df_stage3 rather than chaining every operation together. This makes it easier to identify the first stage where customer_id disappears.
Also remember that Spark uses lazy evaluation. The exception may appear when an action runs, even though the problematic column reference was introduced earlier in the transformation chain.
My suggestion would be to print the schema immediately before the failing operation and compare it with the original schema.
If you can share the transformation involving the joins, selections or aggregations, along with the complete error message, it should be possible to pinpoint exactly where customer_id becomes unresolved.
Official documentation:
https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/dataframe.html