Built with Tutify.io

    AIStudyLabs

    DataFrame - Remove Duplicate Rows

    DataFrame - Remove Duplicate Rows

    Introduction

    Let's see different ways to get the distinct values in every column or select multiple columns in a DataFrame.

    First, let’s create a DataFrame with some duplicate rows and duplicate values in a few columns. We use this DataFrame to demonstrate how to get distinct multiple columns.

    import spark.implicits._

    val simpleData = Seq(
      ("James", "Sales", 3000),
      ("Michael", "Sales", 4600),
      ("Robert", "Sales", 4100),
      ("Maria", "Finance", 3000),
      ("James", "Sales", 3000),
      ("Scott", "Finance", 3300),
      ("Jen", "Finance", 3900),
      ("Jeff", "Marketing", 3000),
      ("Kumar", "Marketing", 2000),
      ("Saif", "Sales", 4100)
    )
    
    val df = simpleData.toDF("employee_name", "department", "salary")
    df.show()
    

    Output:

    +-------------+----------+------+
    |employee_name|department|salary|
    +-------------+----------+------+
    |        James|     Sales|  3000|
    |      Michael|     Sales|  4600|
    |       Robert|     Sales|  4100|
    |        Maria|   Finance|  3000|
    |        James|     Sales|  3000|
    |        Scott|   Finance|  3300|
    |          Jen|   Finance|  3900|
    |         Jeff| Marketing|  3000|
    |        Kumar| Marketing|  2000|
    |         Saif|     Sales|  4100|
    +-------------+----------+------+
    

    As you notice, we have 2 rows that have duplicate values on all columns, and we have 4 rows that have duplicate values on “department” and “salary” columns.

    1. Get Distinct All Columns

    In the above DataFrame, we have a total of 10 rows and one row with all values duplicated. Performing distinct on this DataFrame should get us 9, as we have one duplicate.

    // Distinct all columns
    val distinctDF = df.distinct()
    println("Distinct count: " + distinctDF.count())
    distinctDF.show(false)
    

    The distinct() function on DataFrame returns a new DataFrame after removing the duplicate records. This example yields the below output.

    Output:

    Distinct count: 9
    +-------------+----------+------+
    |employee_name|department|salary|
    +-------------+----------+------+
    |James        |Sales     |3000  |
    |Michael      |Sales     |4600  |
    |Maria        |Finance   |3000  |
    |Robert       |Sales     |4100  |
    |Saif         |Sales     |4100  |
    |Scott        |Finance   |3300  |
    |Jeff         |Marketing |3000  |
    |Jen          |Finance   |3900  |
    |Kumar        |Marketing |2000  |
    +-------------+----------+------+
    

    Alternatively, you can also run the dropDuplicates() function which returns a new DataFrame with duplicate rows removed.

    val df2 = df.dropDuplicates()
    println("Distinct count: " + df2.count())
    df2.show(false)
    

    2. Distinct of Multiple Columns

    Spark doesn’t have a distinct method that takes columns to run distinct on. However, Spark provides another signature of the dropDuplicates() function, which takes multiple columns to eliminate duplicates.

    Calling dropDuplicates() on a DataFrame returns a new DataFrame with duplicate rows removed.

    // Distinct using dropDuplicates
    val dropDisDF = df.dropDuplicates("department", "salary")
    println("Distinct count of department & salary: " + dropDisDF.count())
    dropDisDF.show(false)
    

    This yields the output below. If you notice the output, it dropped 2 records that are duplicates.

    Output:

    Distinct count of department & salary: 8
    +-------------+----------+------+
    |employee_name|department|salary|
    +-------------+----------+------+
    |Jen          |Finance   |3900  |
    |Maria        |Finance   |3000  |
    |Scott        |Finance   |3300  |
    |Michael      |Sales     |4600  |
    |Kumar        |Marketing |2000  |
    |Robert       |Sales     |4100  |
    |James        |Sales     |3000  |
    |Jeff         |Marketing |3000  |
    +-------------+----------+------+
    

    The complete example is available at GitHub for reference.

    Apache Spark Tutorial with Scala Examples — DataFrame Wider Transformation

    Introduction to Apache Spark

    Setup Apache Spark Environment

    SparkSession & SparkContext

    Resilient Distributed Datasets (RDDs)

    Apache Spark Concepts

    Cache, Persistence and Shared Variables

    DataFrame Introduction

    DataFrame Narrow Transformation

    DataFrame Wider Transformation

    DataFrame Actions

    Spark SQL Functions

    Advanced DataFrame Features

    Using SQL Queries

    Spark Read & Write CSV Files

    Working with JSON Files

    Working with Parquet Files

    Spark Dataset

    What Next?

    Tutorial Not Found