How to removes duplicate values from array in PySpark

PySpark @

This blog will show you , how to remove the duplicates in an column with array elements. Consider the below example. In this one of the column we have array elements and we can see duplicates exists . No we need to remove the duplicates from that array.


['India','India','United States','Bahrain','India']
['Argentina','Bahrain','United States','Argentina','India']
['China','Bahrain','United States','Bahrain','Greenland']
['India','Bahrain','United States','Bahrain','Greenland']
['United States','Bahrain','United States','Bahrain']

Pyspark provides array_distinct function to handle this


Syntax : array_distinct(array)

The function will return an array of the same type as the input argument after removing all duplicate values.

Example Code

from pyspark.sql import SparkSession
from pyspark.sql.functions import array_distinct
#Removes duplicate values from array.
spark = SparkSession.builder.appName(' training').getOrCreate()
df = spark.createDataFrame([(['India','India','United States','Bahrain','India'],),(['Argentina','Bahrain','United States','Argentina','India'],) ,(['China','Bahrain','United States','Bahrain','Greenland'],),(['India','Bahrain','United States','Bahrain','Greenland'],),(['United States','Bahrain','United States','Bahrain'],)],['country']),False),False)

Note : We use Python 3.7.10 and Spark 3.0.1

Spark Examples

PySpark Blogs

Bigdata Blogs

Spark Interview Questions


Video Tutorial

Author: user

Leave a Reply