如何在 Pyspark 中替换数据帧的所有空值 [英] How to replace all Null values of a dataframe in Pyspark

查看:14
本文介绍了如何在 Pyspark 中替换数据帧的所有空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 pyspark 中有一个超过 300 列的数据框.在这些列中有一些值为 null 的列.

例如:

Column_1 column_2空空空空第234话125 124365 187等等

当我想对 column_1 求和时,结果是 Null,而不是 724.

现在我想用空白替换数据框所有列中的空值.因此,当我尝试对这些列求和时,我不会得到空值,但会得到一个数值.

我们如何在 pyspark 中实现这一点

解决方案

您可以使用 df.na.fill 将空值替换为零,例如:

<预><代码>>>>df = spark.createDataFrame([(1,), (2,), (3,), (None,)], ['col'])>>>df.show()+----+|颜色|+----+|1||2||3||空|+----+>>>df.na.fill(0).show()+---+|列|+---+|1||2||3||0|+---+

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆