代码之家  ›  专栏  ›  技术社区  ›  Tibberzz

Pyspark熔化空列

  •  1
  • Tibberzz  · 技术社区  · 8 年前

    我有这样一个数据帧:

    # +----+------------+----------+-----------+
    # | id |  c_type_1  | c_type_2 |  c_type_3 |
    # +----+------------+----------+-----------+
    # |  1 |    null    |   null   |     r     |
    # |  2 |     a      |   null   |    null   |
    # |  3 |    null    |   null   |    null   |
    # +---+-------------+----------+-----------+
    

    我需要把它转换成这样:

    # +----+------------+------------+
    # | id |   c_type   | c_type_val |
    # +----+------------+------------+
    # |  1 |  c_type_3  |     r      |
    # |  2 |  c_type_1  |     a      |
    # |  3 |    null    |    null    |
    # +---+-------------+------------+
    

    每行只有一个c\类型值,否则所有c\类型值都将为空。

    我现在就这样把这一排融为一体:

    def melt(df, id_cols, value_cols, c_type, c_value):
        v_arr = []
        for c in value_cols:
            v_arr.append(struct(lit(c).alias(c_type), col(c).alias(c_value)))
    
        vars_and_vals = array(*v_arr)
    
        tmp = df.withColumn("vars_and_vals", explode(vars_and_vals))
    
        cols = id_cols + [
            col("vars_and_vals")[x].alias(x) for x in [c_type, c_value]]
        return tmp.select(*cols)
    
    
    melted = melt(df, df.columns[:1], df.columns[1:4], 'c_type', 'c_type_val')
    
    melted.filter(melted.c_type_val.isNotNull()).show()
    

    问题是,筛选c\u type\u val的空值会筛选出id==3的行(任何c\u type为空的行)。我需要一种方法来融化和过滤,以保持第三行为空的cèu类型和值。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Mohamed Thasin ah    8 年前

    我试过用 pandas ,它可能会给你一个解决这个问题的主意,

    temp=df.filter(like='c_type')
    df= pd.merge(df,temp[temp.notnull()].stack().reset_index(), left_index=True, right_on=['level_0'],how='left')
    df=df[['id','level_1',0]].reset_index(drop=True).rename(columns={'level_1':'c_type',0:'c_type_val'})
    print df
    

    输出:

       id    c_type c_type_val
    0   1  c_type_3          r
    1   2  c_type_1          a
    2   3       NaN        NaN
    
    推荐文章