在 spark 数据框中用 N/A 替换空值
Replace null values with N/A in a spark dataframe
我正在尝试用 N/A 替换空值。我尝试使用以下代码,但其中 none 有效:
df.withColumn("series_name", when($"series_name") === null,"n/a")
.otherwise($series_name)
和
df.withColumn("series_name", when(col("series_name") === null,"n/a")
我错过了什么?
+--------------------+
| series_name|
+--------------------+
|Families of the M...|
| null|
| Ridiculousness|
| null|
| null|
+--------------------+
我更喜欢使用 coalesce
。
from pyspark.sql import functions as f
df.withColumn('series_name', f.expr("coalesce(series_name, 'n/a')"))
您也可以使用 .fillna() 方法:
df.fillna('N/A', subset=['series_name'])
我正在尝试用 N/A 替换空值。我尝试使用以下代码,但其中 none 有效:
df.withColumn("series_name", when($"series_name") === null,"n/a")
.otherwise($series_name)
和
df.withColumn("series_name", when(col("series_name") === null,"n/a")
我错过了什么?
+--------------------+
| series_name|
+--------------------+
|Families of the M...|
| null|
| Ridiculousness|
| null|
| null|
+--------------------+
我更喜欢使用 coalesce
。
from pyspark.sql import functions as f
df.withColumn('series_name', f.expr("coalesce(series_name, 'n/a')"))
您也可以使用 .fillna() 方法:
df.fillna('N/A', subset=['series_name'])