【问题标题】:issue to replace null values in pyspark dataframe替换pyspark数据框中的空值的问题
【发布时间】:2020-04-27 05:26:35
【问题描述】:

我在某些 PySpark 数据帧中将空值替换为 0 时遇到问题。

df1df2 两个数据帧。在col1 上执行join 过程后,我得到一个数据框df,其中包含从df1df2 继承的具有相同列名(可能具有不同值)的两列,比如说df1.dup_col 和@ 987654331@。我对它们每个都有空值,我想用0 中的df1.dup_col 替换它们。

所以,我首先删除 df2.dup_col 列,然后调用

df.fillna({"df1.dup_col":'0'})

但我仍然得到 null 值。所以我尝试了,

df.select("df1.dup_col").na.fill(0)

结果相同。所以我尝试了

df = df.withColumn("df1.dup_col", when(df["df1.dup_col"].isNull(), 0).otherwise(
                                         df["df1.dup_col"]))

没有更好的结果。

我错过了什么吗?

【问题讨论】:

    标签: python dataframe pyspark null


    【解决方案1】:

    你应该这样做:

    df = df.fillna("0", subset = ["dup_col"]) # This is the string 0 
    
    df = df.fillna(0, subset = ["dup_col"]) # This is the number 0 
    

    【讨论】:

      【解决方案2】:

      df = df.fillna({'colName':'value_to_replace'})

      【讨论】:

        猜你喜欢
        • 2018-03-09
        • 1970-01-01
        • 1970-01-01
        • 2019-01-11
        • 1970-01-01
        • 2020-10-23
        • 1970-01-01
        • 2019-05-21
        • 2017-07-07
        相关资源
        最近更新 更多