【发布时间】:2022-12-01 15:49:49
【问题描述】:
I have a dataset which has empty cells, and also cells which contain only spaces (one or more). I want to convert all these cells into Null.
Sample dataset:
data = [("", "CA", " "), ("Julia", "", None),("Robert", " ", None), ("Tom", "NJ", " ")]
df = spark.createDataFrame(data,["name", "state", "code"])
df.show()
I can convert empty cells by:
df = df.select( [F.when(F.col(c)=="", None).otherwise(F.col(c)).alias(c) for c in df.columns] )
df.show()
And cells with one space:
df = df.select( [F.when(F.col(c)==" ", None).otherwise(F.col(c)).alias(c) for c in df.columns] )
df.show()
But, I don't want to repeat the above codes for cells with 2, 3, or more spaces.
Is there any way I can convert those cells at once?
【问题讨论】:
标签: python dataframe pyspark null