Distinct Rows and Distinct Count from Spark Dataframe

In this blog, we will learn how to get distinct values from columns or rows in the Spark dataframe. We will also learn how we can count distinct values. We will be using our same flight data for example.

Distinct Values from Dataframe

Consider that we want to get all combinations of source and destination countries from our data. We can easily do this using the following code.

In spark, we can chain multiple operations one after another. Here we are using where clause with distinct values.

Counting Distinct Values

We can also easily count distinct values by chaining count function after we distinct function.

Using dropDuplicates function

If we want to drop all duplicate rows from the dataframe we can also use “dropDuplicates” function.

I hope this helps. See you soon 🙂

Similar Posts