สร้างเวกเตอร์กระจัดกระจายว่างใน PySpark

ฉันมี dataframe DF1 ที่มีลักษณะดังนี้:

+-------+------+
|user_id|meta  |
+-------+------+
|      1|  null|
|     11|  null|
|     15|  null|
+-------+------+

สคีมา:

root
 |-- user_id: string (nullable = true)
 |-- meta: string (nullable = true)

และฉันมี dataframe DF2 อีกอันที่มีลักษณะเช่นนี้

+-------+------------------------------------+
|user_id|            Vectorz                 |
+-------+------------------------------------+
|     10|                       (2,[1],[1.0])|
|     12|                       (2,[1],[1.0])|
|     13|                       (2,[0],[1.0])|
|     14|                       (2,[1],[1.0])|
---------------------------------------------

สคีมาคือ:

[user_id: string, Vectorz: vector]

ฉันต้องการฉีด user_ids ทั้งหมดจาก DF1 ลงใน DF2 แต่สร้างเวกเตอร์กระจัดกระจายว่างสำหรับพวกเขาเนื่องจากคอลัมน์ "meta" ของพวกเขาเป็น NULL ทั้งหมด

ในที่สุดฉันก็ต้องการให้ DF2 เป็น:

+-------+------------------------------------+
|user_id|            Vectorz                 |
+-------+------------------------------------+
|      1|                            (,[],[])|
|     10|                       (2,[1],[1.0])|
|     11|                            (,[],[])|
|     12|                       (2,[1],[1.0])|
|     13|                       (2,[0],[1.0])|
|     14|                       (2,[1],[1.0])|
|     15|                            (,[],[])|
---------------------------------------------

ใครสามารถช่วยได้บ้าง?

ฉันยังใหม่กับ PySpark ดังนั้นขออภัยหากฉันฟังดูไม่ค่อยมีข้อมูลเพียงพอ


person VictorCreator    schedule 20.05.2020    source แหล่งที่มา
comment
คุณได้ลองอะไร? คุณสามารถลองเพิ่ม Vectorz ใน DF1 วาง meta จากนั้นรวม dfs ทั้งสองเข้าด้วยกัน   -  person moon    schedule 20.05.2020
comment
สวัสดี คุณช่วยตรวจสอบคำตอบของฉันได้ไหม โปรดโหวต + ยอมรับถ้ามันเหมาะกับคุณ   -  person Som    schedule 22.05.2020


คำตอบ (1)


คุณสามารถสร้างเวกเตอร์เปล่าสำหรับ user_ids ทั้งหมดได้เมื่อเมตาเป็นโมฆะ อย่างไรก็ตาม คุณต้องตัดสินใจว่าเมื่อใดที่คอลัมน์เมตาไม่เป็นโมฆะ

ตัวอย่างโค้ด

  1. DF1
val spark = sqlContext.sparkSession
    val implicits = sqlContext.sparkSession.implicits
    import implicits._

    val df1 = sqlContext.range(1,4)
      .withColumnRenamed("id", "user_id")
      .withColumn("meta", lit(null).cast(DataTypes.StringType))
    df1.show(false)
    df1.printSchema()

      +-------+----+
      |user_id|meta|
      +-------+----+
      |1      |null|
      |2      |null|
      |3      |null|
      +-------+----+

    root
    |-- user_id: long (nullable = false)
    |-- meta: string (nullable = true)
  1. DF2
    import org.apache.spark.ml.linalg.Vectors
    val staticVector = udf(() => Vectors.sparse(5, Seq((1, 1.0), (3, 7.0))), SQLDataTypes.VectorType)
    val df2 = sqlContext.range(5,8)
      .withColumnRenamed("id", "user_id")
      .withColumn("Vectorz", staticVector())

    df2.show(false)
    df2.printSchema()

    +-------+-------------------+
    |user_id|Vectorz            |
    +-------+-------------------+
    |5      |(5,[1,3],[1.0,7.0])|
    |6      |(5,[1,3],[1.0,7.0])|
    |7      |(5,[1,3],[1.0,7.0])|
    +-------+-------------------+

    root
    |-- user_id: long (nullable = false)
    |-- Vectorz: vector (nullable = true)
  1. DF ที่ประมวลผลแล้ว
  val emptyVector = udf(() => Vectors.sparse(0, Array.empty[Int], Array.empty[Double]), SQLDataTypes.VectorType)

    val processedDF =
      // meta column shouldn't have any value
      // for the safer side adding filter as meta is null
      // need to decide what if meta is not null
      // I'm assigning empty vector to that also
      df1.where(col("meta").isNull)
        .withColumn("Vectorz", when(col("meta").isNull, emptyVector()).otherwise(emptyVector()))
        .drop("meta")
        .unionByName(df2)

    processedDF.show(false)
    processedDF.printSchema()

    +-------+-------------------+
    |user_id|Vectorz            |
    +-------+-------------------+
    |1      |(0,[],[])          |
    |2      |(0,[],[])          |
    |3      |(0,[],[])          |
    |5      |(5,[1,3],[1.0,7.0])|
    |6      |(5,[1,3],[1.0,7.0])|
    |7      |(5,[1,3],[1.0,7.0])|
    +-------+-------------------+

    root
    |-- user_id: long (nullable = false)
    |-- Vectorz: vector (nullable = true)


person Som    schedule 20.05.2020