Remove blank space from data frame column values in Spark

后端 未结 5 879
既然无缘
既然无缘 2021-01-01 04:39

I have a data frame (business_df) of schema:

|-- business_id: string (nullable = true)
|-- categories: array (nullable = true)
|    |-- element         


        
5条回答
  •  悲&欢浪女
    2021-01-01 05:29

    While the problem you've described is not reproducible with provided code, using Python UDFs to handle simple tasks like this, is rather inefficient. If you want to simply remove spaces from the text use regexp_replace:

    from pyspark.sql.functions import regexp_replace, col
    
    df = sc.parallelize([
        (1, "foo bar"), (2, "foobar "), (3, "   ")
    ]).toDF(["k", "v"])
    
    df.select(regexp_replace(col("v"), " ", ""))
    

    If you want to normalize empty lines use trim:

    from pyspark.sql.functions import trim
    
    df.select(trim(col("v")))
    

    If you want to keep leading / trailing spaces you can adjust regexp_replace:

    df.select(regexp_replace(col("v"), "^\s+$", ""))
    

提交回复
热议问题