gpt4 book ai didi

apache-spark - 在pyspark中,是否可以用另一列填充?

转载 作者:行者123 更新时间:2023-12-01 11:15:30 25 4
gpt4 key购买 nike

假设有一个像这样的 RDD:

+----+--------------+-----+
| age|best_guess_age| name|
+----+--------------+-----+
| 23| 23|Alice|
|null| 18| Bob|
| 34| 32| Tom|
|null| 40|Linda|
+----+--------------+-----+

我们要填写的地方 age列与 best_guess_age列为空时。

fillna command需要一个实际值来替换 na,我们不能简单地传入一列。

这该怎么做?

最佳答案

您可以使用 coalesce 功能;通过做 coalesce('age', 'best_guess_age') ,它将采用来自 age 的值列如果不为空,否则来自 best_guess_age柱子:

from pyspark.sql.functions import coalesce
df.withColumn('age', coalesce('age', 'best_guess_age')).show()
+---+--------------+-----+
|age|best_guess_age| name|
+---+--------------+-----+
| 23| 23|Alice|
| 18| 18| Bob|
| 34| 32| Tom|
| 40| 40|Linda|
+---+--------------+-----+

关于apache-spark - 在pyspark中,是否可以用另一列填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51952584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com