如何在Pyspark中替换数据框的所有Null值 [英] How to replace all Null values of a dataframe in Pyspark
本文介绍了如何在Pyspark中替换数据框的所有Null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在pyspark中有一个数据框,其中包含300多个列。在这些列中,有些列的值为null。
I have a data frame in pyspark with more than 300 columns. In these columns there are some columns with values null.
例如:
Column_1 column_2
null null
null null
234 null
125 124
365 187
and so on
当我想对column_1求和时,得到的是Null,而不是724。
When I want to do a sum of column_1 I am getting a Null as a result, instead of 724.
现在我想要将数据框所有列中的null替换为空白。因此,当我尝试对这些列求和时,我不会得到空值,但会得到一个数值。
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.
我们如何在pyspark
How can we achieve that in pyspark
推荐答案
您可以使用 df.na.fill
将零替换为零,例如:
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中替换数据框的所有Null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文