PySpark
DataFrame
String to Double
Type Conversion
Data Processing

How to change a dataframe column from String type to Double type in PySpark?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Converting a PySpark column from string to double is a standard data-cleaning step before analytics, aggregations, or model training. A direct cast works only when values are already clean numeric strings, so robust pipelines usually normalize the input first. The best pattern is inspect, sanitize, cast, then validate null rates and range checks.

Inspect the Current Schema and Data Quality

Start by confirming the incoming schema and sampling problematic rows. This prevents silent conversion issues later.

python
1from pyspark.sql import SparkSession
2from pyspark.sql import functions as F
3
4spark = SparkSession.builder.appName("cast-demo").getOrCreate()
5
6data = [
7    ("1", "19.99"),
8    ("2", " 42.5 "),
9    ("3", "1,250.75"),
10    ("4", "N/A"),
11    ("5", None),
12]
13
14df = spark.createDataFrame(data, ["id", "amount_str"])
15df.printSchema()
16df.show(truncate=False)

You can immediately see values with spaces, thousands separators, and non-numeric text.

Convert String to Double Safely

For reliable casting, trim whitespace and remove separators first, then cast.

python
1clean_df = (
2    df
3    .withColumn("amount_trim", F.trim(F.col("amount_str")))
4    .withColumn("amount_norm", F.regexp_replace(F.col("amount_trim"), ",", ""))
5    .withColumn("amount", F.col("amount_norm").cast("double"))
6)
7
8clean_df.select("id", "amount_str", "amount").show(truncate=False)
9clean_df.printSchema()

When conversion fails, Spark returns null. That is useful because you can detect bad records and route them for cleanup.

Keep Only Valid Rows or Track Invalid Rows

Choose a strategy based on business needs. Some pipelines can drop invalid values, while regulated workflows may need a rejection table.

python
1valid_df = clean_df.filter(F.col("amount").isNotNull())
2invalid_df = clean_df.filter(F.col("amount").isNull())
3
4print("valid rows:", valid_df.count())
5print("invalid rows:", invalid_df.count())
6
7invalid_df.select("id", "amount_str").show(truncate=False)

If zero is acceptable as a fallback, you can fill nulls. Do this only when the downstream meaning is clear.

python
filled_df = clean_df.fillna({"amount": 0.0})
filled_df.select("id", "amount_str", "amount").show(truncate=False)

SQL Expression Alternative

In teams that prefer SQL transformations, you can perform the same conversion using expr.

python
1sql_df = df.selectExpr(
2    "id",
3    "amount_str",
4    "CAST(REGEXP_REPLACE(TRIM(amount_str), ',', '') AS DOUBLE) AS amount"
5)
6
7sql_df.show(truncate=False)

This form is useful when porting logic from warehouse SQL or documenting transformations in one expression.

Validation Checks for Production Pipelines

After casting, run lightweight checks so type conversion failures do not slip into reports.

python
1metrics = clean_df.select(
2    F.count("*").alias("total_rows"),
3    F.sum(F.when(F.col("amount").isNull(), 1).otherwise(0)).alias("null_amount_rows"),
4    F.min("amount").alias("min_amount"),
5    F.max("amount").alias("max_amount")
6)
7
8metrics.show(truncate=False)

Store these metrics with your pipeline run metadata. If null rates spike unexpectedly, fail fast and alert.

Handle Currency Symbols and Locale Noise

Real feeds often contain currency signs and mixed separators. Normalize with regex before casting, then keep a copy of the raw value for audits.

python
1currency_df = df.withColumn(
2    "amount_norm",
3    F.regexp_replace(F.trim(F.col("amount_str")), r"[^0-9,.-]", "")
4).withColumn(
5    "amount_norm",
6    F.regexp_replace(F.col("amount_norm"), ",", "")
7).withColumn(
8    "amount",
9    F.col("amount_norm").cast("double")
10)
11
12currency_df.select("id", "amount_str", "amount_norm", "amount").show(truncate=False)

This approach is deterministic and easy to review during debugging. In scheduled jobs, persist both amount_str and amount_norm in intermediate tables for a short retention period. That gives data engineers a fast path to compare source values with normalized values when alerts fire.

Common Pitfalls

  • Casting raw strings directly and ignoring format noise such as commas or extra spaces.
  • Treating conversion nulls as harmless. They often indicate upstream feed issues.
  • Filling null with zero without confirming business semantics.
  • Mixing locale-specific decimal formats in one column without normalization rules.
  • Skipping validation metrics, which hides data quality regressions until much later.

Summary

  • Inspect schema and sample values before casting.
  • Normalize strings with trim and regexp_replace before cast('double').
  • Separate valid and invalid rows so data quality is explicit.
  • Use SQL expressions when that style fits your pipeline conventions.
  • Add null-rate and range checks to catch conversion problems early.

Course illustration
Course illustration

All Rights Reserved.