Problems / Type Casting / Editorial
When data is imported as strings (common with CSV files), you must explicitly cast to the correct types before performing calculations.
```sql
CAST(amount_str AS DOUBLE) AS amount
```
The total column demonstrates why type casting matters — you can't multiply strings. After casting, ROUND(amount * quantity, 2) computes the total.
total
ROUND(amount * quantity, 2)
Use .cast() on columns:
.cast()
```python
F.col("amount_str").cast("double")
Note that .withColumn() lets you reference previously created columns (like amount) in subsequent calls within the same chain.
.withColumn()
amount
Solve Type Casting yourself →