Scala: Delete empty array values from a Spark DataFrame











up vote
-2
down vote

favorite
1












I'm a new learner of Scala. Now given a DataFrame named df as follows:



+-------+-------+-------+-------+
|Column1|Column2|Column3|Column4|
+-------+-------+-------+-------+
| [null]| [0.0]| [0.0]| [null]|
| [IND1]| [5.0]| [6.0]| [A]|
| [IND2]| [7.0]| [8.0]| [B]|
| | | | |
+-------+-------+-------+-------+


I'd like to delete rows if all columns is an empty array (4th row).



For example I might expect the result is:



+-------+-------+-------+-------+
|Column1|Column2|Column3|Column4|
+-------+-------+-------+-------+
| [null]| [0.0]| [0.0]| [null]|
| [IND1]| [5.0]| [6.0]| [A]|
| [IND2]| [7.0]| [8.0]| [B]|
+-------+-------+-------+-------+


I'm trying to use isNotNull (like val temp=df.filter(col("Column1").isNotNull && col("Column2").isNotNull && col("Column3").isNotNull && col("Column4").isNotNull).show()
) but still show all rows.



I found python solution of using a Hive UDF from link, but I had hard time trying to convert to a valid scala code. I would like use scala command similar to the following code:



val query = "SELECT * FROM targetDf WHERE {0}".format(" AND ".join("SIZE({0}) > 0".format(c) for c in ["Column1", "Column2", "Column3","Column4"]))
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
sqlContext.sql(query)


Any help would be appreciated. Thank you.










share|improve this question


























    up vote
    -2
    down vote

    favorite
    1












    I'm a new learner of Scala. Now given a DataFrame named df as follows:



    +-------+-------+-------+-------+
    |Column1|Column2|Column3|Column4|
    +-------+-------+-------+-------+
    | [null]| [0.0]| [0.0]| [null]|
    | [IND1]| [5.0]| [6.0]| [A]|
    | [IND2]| [7.0]| [8.0]| [B]|
    | | | | |
    +-------+-------+-------+-------+


    I'd like to delete rows if all columns is an empty array (4th row).



    For example I might expect the result is:



    +-------+-------+-------+-------+
    |Column1|Column2|Column3|Column4|
    +-------+-------+-------+-------+
    | [null]| [0.0]| [0.0]| [null]|
    | [IND1]| [5.0]| [6.0]| [A]|
    | [IND2]| [7.0]| [8.0]| [B]|
    +-------+-------+-------+-------+


    I'm trying to use isNotNull (like val temp=df.filter(col("Column1").isNotNull && col("Column2").isNotNull && col("Column3").isNotNull && col("Column4").isNotNull).show()
    ) but still show all rows.



    I found python solution of using a Hive UDF from link, but I had hard time trying to convert to a valid scala code. I would like use scala command similar to the following code:



    val query = "SELECT * FROM targetDf WHERE {0}".format(" AND ".join("SIZE({0}) > 0".format(c) for c in ["Column1", "Column2", "Column3","Column4"]))
    val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    sqlContext.sql(query)


    Any help would be appreciated. Thank you.










    share|improve this question
























      up vote
      -2
      down vote

      favorite
      1









      up vote
      -2
      down vote

      favorite
      1






      1





      I'm a new learner of Scala. Now given a DataFrame named df as follows:



      +-------+-------+-------+-------+
      |Column1|Column2|Column3|Column4|
      +-------+-------+-------+-------+
      | [null]| [0.0]| [0.0]| [null]|
      | [IND1]| [5.0]| [6.0]| [A]|
      | [IND2]| [7.0]| [8.0]| [B]|
      | | | | |
      +-------+-------+-------+-------+


      I'd like to delete rows if all columns is an empty array (4th row).



      For example I might expect the result is:



      +-------+-------+-------+-------+
      |Column1|Column2|Column3|Column4|
      +-------+-------+-------+-------+
      | [null]| [0.0]| [0.0]| [null]|
      | [IND1]| [5.0]| [6.0]| [A]|
      | [IND2]| [7.0]| [8.0]| [B]|
      +-------+-------+-------+-------+


      I'm trying to use isNotNull (like val temp=df.filter(col("Column1").isNotNull && col("Column2").isNotNull && col("Column3").isNotNull && col("Column4").isNotNull).show()
      ) but still show all rows.



      I found python solution of using a Hive UDF from link, but I had hard time trying to convert to a valid scala code. I would like use scala command similar to the following code:



      val query = "SELECT * FROM targetDf WHERE {0}".format(" AND ".join("SIZE({0}) > 0".format(c) for c in ["Column1", "Column2", "Column3","Column4"]))
      val sqlContext = new org.apache.spark.sql.SQLContext(sc)
      sqlContext.sql(query)


      Any help would be appreciated. Thank you.










      share|improve this question













      I'm a new learner of Scala. Now given a DataFrame named df as follows:



      +-------+-------+-------+-------+
      |Column1|Column2|Column3|Column4|
      +-------+-------+-------+-------+
      | [null]| [0.0]| [0.0]| [null]|
      | [IND1]| [5.0]| [6.0]| [A]|
      | [IND2]| [7.0]| [8.0]| [B]|
      | | | | |
      +-------+-------+-------+-------+


      I'd like to delete rows if all columns is an empty array (4th row).



      For example I might expect the result is:



      +-------+-------+-------+-------+
      |Column1|Column2|Column3|Column4|
      +-------+-------+-------+-------+
      | [null]| [0.0]| [0.0]| [null]|
      | [IND1]| [5.0]| [6.0]| [A]|
      | [IND2]| [7.0]| [8.0]| [B]|
      +-------+-------+-------+-------+


      I'm trying to use isNotNull (like val temp=df.filter(col("Column1").isNotNull && col("Column2").isNotNull && col("Column3").isNotNull && col("Column4").isNotNull).show()
      ) but still show all rows.



      I found python solution of using a Hive UDF from link, but I had hard time trying to convert to a valid scala code. I would like use scala command similar to the following code:



      val query = "SELECT * FROM targetDf WHERE {0}".format(" AND ".join("SIZE({0}) > 0".format(c) for c in ["Column1", "Column2", "Column3","Column4"]))
      val sqlContext = new org.apache.spark.sql.SQLContext(sc)
      sqlContext.sql(query)


      Any help would be appreciated. Thank you.







      arrays scala apache-spark dataframe user-defined-functions






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 18:08









      Haven Shi

      7728




      7728
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Using the isNotNull or isNull will not work because it is looking for a 'null' value in the DataFrame. Your example DF does not contain null values but empty values, there is a difference there.



          One option: You could create a new column that has the length of of the array and filter for if the array is zero.



            val dfFil = df
          .withColumn("arrayLengthColOne", size($"Column1"))
          .withColumn("arrayLengthColTwo", size($"Column2"))
          .withColumn("arrayLengthColThree", size($"Column3"))
          .withColumn("arrayLengthColFour", size($"Column4"))
          .filter($"arrayLengthColOne" =!= 0 && $"arrayLengthColTwo" =!= 0
          && $"arrayLengthColThree" =!= 0 && $"arrayLengthColFour" =!= 0)
          .drop("arrayLengthColOne", "arrayLengthColTwo", "arrayLengthColThree", "arrayLengthColFour")


          Original DF:



          +-------+-------+-------+-------+
          |Column1|Column2|Column3|Column4|
          +-------+-------+-------+-------+
          | [A]| [B]| [C]| [d]|
          | | | | |
          +-------+-------+-------+-------+


          New DF:



          +-------+-------+-------+-------+
          |Column1|Column2|Column3|Column4|
          +-------+-------+-------+-------+
          | [A]| [B]| [C]| [d]|
          +-------+-------+-------+-------+


          You could also create a function that will map across all the columns and do it.






          share|improve this answer























          • Hi fletchr thanks for the reply. It is a very good direction. My only concern is, this withColumn filter condition only check size($"Column1"), therefore it may delete | | [0.0]| [0.0]| [A]|. Is there anyway to modify condition as filter sum of size($"Column1"), size($"Column2"), size($"Column3") and size($"Column4")?
            – Haven Shi
            Nov 11 at 19:09










          • Let me know if this clarifies your problem. It is a bit iterative and cumbersome, but I think it will solve your problem. I just edited my answer. I'll let you find out a more elegant solution
            – fletchr
            Nov 11 at 19:30












          • Thank you for the update, I think it should be a correct answer. However on my side it shows "value && is not a member of Int" on every $$ symbol, have you seen that error before?
            – Haven Shi
            Nov 11 at 19:54










          • @HavenShi try import spark.implicits._ or instead of suing $ you can use col("columnOfInterest")
            – fletchr
            Nov 11 at 20:52




















          up vote
          1
          down vote













          Another approach (in addition to accepted answer) would be using Datasets.

          For example, by having a case class:



          case class MyClass(col1: Seq[String],
          col2: Seq[Double],
          col3: Seq[Double],
          col4: Seq[String]) {
          def isEmpty: Boolean = ...
          }


          You can represent your source as a typed structure:



          import spark.implicits._ // needed to provide an implicit encoder/data mapper 

          val originalSource: DataFrame = ... // provide your source
          val source: Dataset[MyClass] = originalSource.as[MyClass] // convert/map it to Dataset


          So you could do filtering like following:



          source.filter(element => !element.isEmpty) // calling class's instance method





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251681%2fscala-delete-empty-array-values-from-a-spark-dataframe%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            Using the isNotNull or isNull will not work because it is looking for a 'null' value in the DataFrame. Your example DF does not contain null values but empty values, there is a difference there.



            One option: You could create a new column that has the length of of the array and filter for if the array is zero.



              val dfFil = df
            .withColumn("arrayLengthColOne", size($"Column1"))
            .withColumn("arrayLengthColTwo", size($"Column2"))
            .withColumn("arrayLengthColThree", size($"Column3"))
            .withColumn("arrayLengthColFour", size($"Column4"))
            .filter($"arrayLengthColOne" =!= 0 && $"arrayLengthColTwo" =!= 0
            && $"arrayLengthColThree" =!= 0 && $"arrayLengthColFour" =!= 0)
            .drop("arrayLengthColOne", "arrayLengthColTwo", "arrayLengthColThree", "arrayLengthColFour")


            Original DF:



            +-------+-------+-------+-------+
            |Column1|Column2|Column3|Column4|
            +-------+-------+-------+-------+
            | [A]| [B]| [C]| [d]|
            | | | | |
            +-------+-------+-------+-------+


            New DF:



            +-------+-------+-------+-------+
            |Column1|Column2|Column3|Column4|
            +-------+-------+-------+-------+
            | [A]| [B]| [C]| [d]|
            +-------+-------+-------+-------+


            You could also create a function that will map across all the columns and do it.






            share|improve this answer























            • Hi fletchr thanks for the reply. It is a very good direction. My only concern is, this withColumn filter condition only check size($"Column1"), therefore it may delete | | [0.0]| [0.0]| [A]|. Is there anyway to modify condition as filter sum of size($"Column1"), size($"Column2"), size($"Column3") and size($"Column4")?
              – Haven Shi
              Nov 11 at 19:09










            • Let me know if this clarifies your problem. It is a bit iterative and cumbersome, but I think it will solve your problem. I just edited my answer. I'll let you find out a more elegant solution
              – fletchr
              Nov 11 at 19:30












            • Thank you for the update, I think it should be a correct answer. However on my side it shows "value && is not a member of Int" on every $$ symbol, have you seen that error before?
              – Haven Shi
              Nov 11 at 19:54










            • @HavenShi try import spark.implicits._ or instead of suing $ you can use col("columnOfInterest")
              – fletchr
              Nov 11 at 20:52

















            up vote
            1
            down vote



            accepted










            Using the isNotNull or isNull will not work because it is looking for a 'null' value in the DataFrame. Your example DF does not contain null values but empty values, there is a difference there.



            One option: You could create a new column that has the length of of the array and filter for if the array is zero.



              val dfFil = df
            .withColumn("arrayLengthColOne", size($"Column1"))
            .withColumn("arrayLengthColTwo", size($"Column2"))
            .withColumn("arrayLengthColThree", size($"Column3"))
            .withColumn("arrayLengthColFour", size($"Column4"))
            .filter($"arrayLengthColOne" =!= 0 && $"arrayLengthColTwo" =!= 0
            && $"arrayLengthColThree" =!= 0 && $"arrayLengthColFour" =!= 0)
            .drop("arrayLengthColOne", "arrayLengthColTwo", "arrayLengthColThree", "arrayLengthColFour")


            Original DF:



            +-------+-------+-------+-------+
            |Column1|Column2|Column3|Column4|
            +-------+-------+-------+-------+
            | [A]| [B]| [C]| [d]|
            | | | | |
            +-------+-------+-------+-------+


            New DF:



            +-------+-------+-------+-------+
            |Column1|Column2|Column3|Column4|
            +-------+-------+-------+-------+
            | [A]| [B]| [C]| [d]|
            +-------+-------+-------+-------+


            You could also create a function that will map across all the columns and do it.






            share|improve this answer























            • Hi fletchr thanks for the reply. It is a very good direction. My only concern is, this withColumn filter condition only check size($"Column1"), therefore it may delete | | [0.0]| [0.0]| [A]|. Is there anyway to modify condition as filter sum of size($"Column1"), size($"Column2"), size($"Column3") and size($"Column4")?
              – Haven Shi
              Nov 11 at 19:09










            • Let me know if this clarifies your problem. It is a bit iterative and cumbersome, but I think it will solve your problem. I just edited my answer. I'll let you find out a more elegant solution
              – fletchr
              Nov 11 at 19:30












            • Thank you for the update, I think it should be a correct answer. However on my side it shows "value && is not a member of Int" on every $$ symbol, have you seen that error before?
              – Haven Shi
              Nov 11 at 19:54










            • @HavenShi try import spark.implicits._ or instead of suing $ you can use col("columnOfInterest")
              – fletchr
              Nov 11 at 20:52















            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            Using the isNotNull or isNull will not work because it is looking for a 'null' value in the DataFrame. Your example DF does not contain null values but empty values, there is a difference there.



            One option: You could create a new column that has the length of of the array and filter for if the array is zero.



              val dfFil = df
            .withColumn("arrayLengthColOne", size($"Column1"))
            .withColumn("arrayLengthColTwo", size($"Column2"))
            .withColumn("arrayLengthColThree", size($"Column3"))
            .withColumn("arrayLengthColFour", size($"Column4"))
            .filter($"arrayLengthColOne" =!= 0 && $"arrayLengthColTwo" =!= 0
            && $"arrayLengthColThree" =!= 0 && $"arrayLengthColFour" =!= 0)
            .drop("arrayLengthColOne", "arrayLengthColTwo", "arrayLengthColThree", "arrayLengthColFour")


            Original DF:



            +-------+-------+-------+-------+
            |Column1|Column2|Column3|Column4|
            +-------+-------+-------+-------+
            | [A]| [B]| [C]| [d]|
            | | | | |
            +-------+-------+-------+-------+


            New DF:



            +-------+-------+-------+-------+
            |Column1|Column2|Column3|Column4|
            +-------+-------+-------+-------+
            | [A]| [B]| [C]| [d]|
            +-------+-------+-------+-------+


            You could also create a function that will map across all the columns and do it.






            share|improve this answer














            Using the isNotNull or isNull will not work because it is looking for a 'null' value in the DataFrame. Your example DF does not contain null values but empty values, there is a difference there.



            One option: You could create a new column that has the length of of the array and filter for if the array is zero.



              val dfFil = df
            .withColumn("arrayLengthColOne", size($"Column1"))
            .withColumn("arrayLengthColTwo", size($"Column2"))
            .withColumn("arrayLengthColThree", size($"Column3"))
            .withColumn("arrayLengthColFour", size($"Column4"))
            .filter($"arrayLengthColOne" =!= 0 && $"arrayLengthColTwo" =!= 0
            && $"arrayLengthColThree" =!= 0 && $"arrayLengthColFour" =!= 0)
            .drop("arrayLengthColOne", "arrayLengthColTwo", "arrayLengthColThree", "arrayLengthColFour")


            Original DF:



            +-------+-------+-------+-------+
            |Column1|Column2|Column3|Column4|
            +-------+-------+-------+-------+
            | [A]| [B]| [C]| [d]|
            | | | | |
            +-------+-------+-------+-------+


            New DF:



            +-------+-------+-------+-------+
            |Column1|Column2|Column3|Column4|
            +-------+-------+-------+-------+
            | [A]| [B]| [C]| [d]|
            +-------+-------+-------+-------+


            You could also create a function that will map across all the columns and do it.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 19:30

























            answered Nov 11 at 18:34









            fletchr

            13512




            13512












            • Hi fletchr thanks for the reply. It is a very good direction. My only concern is, this withColumn filter condition only check size($"Column1"), therefore it may delete | | [0.0]| [0.0]| [A]|. Is there anyway to modify condition as filter sum of size($"Column1"), size($"Column2"), size($"Column3") and size($"Column4")?
              – Haven Shi
              Nov 11 at 19:09










            • Let me know if this clarifies your problem. It is a bit iterative and cumbersome, but I think it will solve your problem. I just edited my answer. I'll let you find out a more elegant solution
              – fletchr
              Nov 11 at 19:30












            • Thank you for the update, I think it should be a correct answer. However on my side it shows "value && is not a member of Int" on every $$ symbol, have you seen that error before?
              – Haven Shi
              Nov 11 at 19:54










            • @HavenShi try import spark.implicits._ or instead of suing $ you can use col("columnOfInterest")
              – fletchr
              Nov 11 at 20:52




















            • Hi fletchr thanks for the reply. It is a very good direction. My only concern is, this withColumn filter condition only check size($"Column1"), therefore it may delete | | [0.0]| [0.0]| [A]|. Is there anyway to modify condition as filter sum of size($"Column1"), size($"Column2"), size($"Column3") and size($"Column4")?
              – Haven Shi
              Nov 11 at 19:09










            • Let me know if this clarifies your problem. It is a bit iterative and cumbersome, but I think it will solve your problem. I just edited my answer. I'll let you find out a more elegant solution
              – fletchr
              Nov 11 at 19:30












            • Thank you for the update, I think it should be a correct answer. However on my side it shows "value && is not a member of Int" on every $$ symbol, have you seen that error before?
              – Haven Shi
              Nov 11 at 19:54










            • @HavenShi try import spark.implicits._ or instead of suing $ you can use col("columnOfInterest")
              – fletchr
              Nov 11 at 20:52


















            Hi fletchr thanks for the reply. It is a very good direction. My only concern is, this withColumn filter condition only check size($"Column1"), therefore it may delete | | [0.0]| [0.0]| [A]|. Is there anyway to modify condition as filter sum of size($"Column1"), size($"Column2"), size($"Column3") and size($"Column4")?
            – Haven Shi
            Nov 11 at 19:09




            Hi fletchr thanks for the reply. It is a very good direction. My only concern is, this withColumn filter condition only check size($"Column1"), therefore it may delete | | [0.0]| [0.0]| [A]|. Is there anyway to modify condition as filter sum of size($"Column1"), size($"Column2"), size($"Column3") and size($"Column4")?
            – Haven Shi
            Nov 11 at 19:09












            Let me know if this clarifies your problem. It is a bit iterative and cumbersome, but I think it will solve your problem. I just edited my answer. I'll let you find out a more elegant solution
            – fletchr
            Nov 11 at 19:30






            Let me know if this clarifies your problem. It is a bit iterative and cumbersome, but I think it will solve your problem. I just edited my answer. I'll let you find out a more elegant solution
            – fletchr
            Nov 11 at 19:30














            Thank you for the update, I think it should be a correct answer. However on my side it shows "value && is not a member of Int" on every $$ symbol, have you seen that error before?
            – Haven Shi
            Nov 11 at 19:54




            Thank you for the update, I think it should be a correct answer. However on my side it shows "value && is not a member of Int" on every $$ symbol, have you seen that error before?
            – Haven Shi
            Nov 11 at 19:54












            @HavenShi try import spark.implicits._ or instead of suing $ you can use col("columnOfInterest")
            – fletchr
            Nov 11 at 20:52






            @HavenShi try import spark.implicits._ or instead of suing $ you can use col("columnOfInterest")
            – fletchr
            Nov 11 at 20:52














            up vote
            1
            down vote













            Another approach (in addition to accepted answer) would be using Datasets.

            For example, by having a case class:



            case class MyClass(col1: Seq[String],
            col2: Seq[Double],
            col3: Seq[Double],
            col4: Seq[String]) {
            def isEmpty: Boolean = ...
            }


            You can represent your source as a typed structure:



            import spark.implicits._ // needed to provide an implicit encoder/data mapper 

            val originalSource: DataFrame = ... // provide your source
            val source: Dataset[MyClass] = originalSource.as[MyClass] // convert/map it to Dataset


            So you could do filtering like following:



            source.filter(element => !element.isEmpty) // calling class's instance method





            share|improve this answer



























              up vote
              1
              down vote













              Another approach (in addition to accepted answer) would be using Datasets.

              For example, by having a case class:



              case class MyClass(col1: Seq[String],
              col2: Seq[Double],
              col3: Seq[Double],
              col4: Seq[String]) {
              def isEmpty: Boolean = ...
              }


              You can represent your source as a typed structure:



              import spark.implicits._ // needed to provide an implicit encoder/data mapper 

              val originalSource: DataFrame = ... // provide your source
              val source: Dataset[MyClass] = originalSource.as[MyClass] // convert/map it to Dataset


              So you could do filtering like following:



              source.filter(element => !element.isEmpty) // calling class's instance method





              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                Another approach (in addition to accepted answer) would be using Datasets.

                For example, by having a case class:



                case class MyClass(col1: Seq[String],
                col2: Seq[Double],
                col3: Seq[Double],
                col4: Seq[String]) {
                def isEmpty: Boolean = ...
                }


                You can represent your source as a typed structure:



                import spark.implicits._ // needed to provide an implicit encoder/data mapper 

                val originalSource: DataFrame = ... // provide your source
                val source: Dataset[MyClass] = originalSource.as[MyClass] // convert/map it to Dataset


                So you could do filtering like following:



                source.filter(element => !element.isEmpty) // calling class's instance method





                share|improve this answer














                Another approach (in addition to accepted answer) would be using Datasets.

                For example, by having a case class:



                case class MyClass(col1: Seq[String],
                col2: Seq[Double],
                col3: Seq[Double],
                col4: Seq[String]) {
                def isEmpty: Boolean = ...
                }


                You can represent your source as a typed structure:



                import spark.implicits._ // needed to provide an implicit encoder/data mapper 

                val originalSource: DataFrame = ... // provide your source
                val source: Dataset[MyClass] = originalSource.as[MyClass] // convert/map it to Dataset


                So you could do filtering like following:



                source.filter(element => !element.isEmpty) // calling class's instance method






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 at 13:25

























                answered Nov 12 at 10:27









                machine head

                314




                314






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251681%2fscala-delete-empty-array-values-from-a-spark-dataframe%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues