Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-23074

Dataframe-ified zipwithindex

    XMLWordPrintableJSON

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Minor
    • Resolution: Incomplete
    • Affects Version/s: 2.3.0
    • Fix Version/s: None
    • Component/s: Spark Core

      Description

      Would be great to have a daraframe-friendly equivalent of rdd.zipWithIndex():

      import org.apache.spark.sql.DataFrame
      import org.apache.spark.sql.types.{LongType, StructField, StructType}
      import org.apache.spark.sql.Row
      
      
      def dfZipWithIndex(
        df: DataFrame,
        offset: Int = 1,
        colName: String = "id",
        inFront: Boolean = true
      ) : DataFrame = {
        df.sqlContext.createDataFrame(
          df.rdd.zipWithIndex.map(ln =>
            Row.fromSeq(
              (if (inFront) Seq(ln._2 + offset) else Seq())
                ++ ln._1.toSeq ++
              (if (inFront) Seq() else Seq(ln._2 + offset))
            )
          ),
          StructType(
            (if (inFront) Array(StructField(colName,LongType,false)) else Array[StructField]()) 
              ++ df.schema.fields ++ 
            (if (inFront) Array[StructField]() else Array(StructField(colName,LongType,false)))
          )
        ) 
      }
      

      credits: https://stackoverflow.com/questions/30304810/dataframe-ified-zipwithindex

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                Tagar Ruslan Dautkhanov
              • Votes:
                6 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: