如何替换 PySpark 中另一列的缺失值?
how to replace missing values from another column in PySpark?
我想用 t5 中的值替换 t4 中的一些缺失值。搜索代码,但对我不起作用
当前:
example of current
目标:
example of target
df 是一个 dataframe.Code:
pdf = df.toPandas()
from pyspark.sql.functions import coalesce
pdf.withColumn("t4", coalesce(pdf.t4, pdf.t5))
错误:'DataFrame'对象没有属性'withColumn'
另外,之前试过下面的代码,也没用。
new_pdf=pdf['t4'].fillna(method='bfill', axis="columns")
错误:没有针对对象类型
的轴命名列
如错误指示 .withColumn() 不是 pandas 数据帧的方法,而是 spark 数据帧的方法。请注意,当使用 .toPandas() 时,您的 pdf 将变为 pandas 数据框,因此如果您想使用 .withColumn() 避免转型
更新:
如果 pdf 是 pandas 数据框,你可以这样做:
pdf['t4']=pdf['t4'].fillna(pdf['t5'])
我想用 t5 中的值替换 t4 中的一些缺失值。搜索代码,但对我不起作用
当前: example of current
目标:
example of target
df 是一个 dataframe.Code:
pdf = df.toPandas()
from pyspark.sql.functions import coalesce
pdf.withColumn("t4", coalesce(pdf.t4, pdf.t5))
错误:'DataFrame'对象没有属性'withColumn'
另外,之前试过下面的代码,也没用。
new_pdf=pdf['t4'].fillna(method='bfill', axis="columns")
错误:没有针对对象类型
的轴命名列如错误指示 .withColumn() 不是 pandas 数据帧的方法,而是 spark 数据帧的方法。请注意,当使用 .toPandas() 时,您的 pdf 将变为 pandas 数据框,因此如果您想使用 .withColumn() 避免转型
更新: 如果 pdf 是 pandas 数据框,你可以这样做:
pdf['t4']=pdf['t4'].fillna(pdf['t5'])