如何在 Pyspark 中替换数据帧的所有空值 [英] How to replace all Null values of a dataframe in Pyspark
问题描述
我在 pyspark 中有一个超过 300 列的数据框.在这些列中有一些值为 null 的列.
例如:
Column_1 column_2空空空空第234话125 124365 187等等
当我想对 column_1 求和时,结果是 Null,而不是 724.
现在我想用空白替换数据框所有列中的空值.因此,当我尝试对这些列求和时,我不会得到空值,但会得到一个数值.
我们如何在 pyspark 中实现这一点
您可以使用 df.na.fill
将空值替换为零,例如:
I have a data frame in pyspark with more than 300 columns. In these columns there are some columns with values null.
For example:
Column_1 column_2
null null
null null
234 null
125 124
365 187
and so on
When I want to do a sum of column_1 I am getting a Null as a result, instead of 724.
Now I want to replace the null in all columns of the data frame with empty space. So when I try to do a sum of these columns I don't get a null value but I will get a numerical value.
How can we achieve that in pyspark
You can use df.na.fill
to replace nulls with zeros, for example:
>>> df = spark.createDataFrame([(1,), (2,), (3,), (None,)], ['col'])
>>> df.show()
+----+
| col|
+----+
| 1|
| 2|
| 3|
|null|
+----+
>>> df.na.fill(0).show()
+---+
|col|
+---+
| 1|
| 2|
| 3|
| 0|
+---+
这篇关于如何在 Pyspark 中替换数据帧的所有空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!