VOOZH about

URL: https://www.geeksforgeeks.org/python/python-pyspark-union-and-unionall/

⇱ Python PySpark - Union and UnionAll - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python PySpark - Union and UnionAll

Last Updated : 21 Feb, 2022

In this article, we will discuss Union and UnionAll in PySpark in Python.

Union in PySpark

The PySpark union() function is used to combine two or more data frames having the same structure or schema. This function returns an error if the schema of data frames differs from each other.

Syntax:

dataFrame1.union(dataFrame2)

Here,

  • dataFrame1 and dataFrame2 are the dataframes

Example 1:

In this example, we have combined two data frames, data_frame1 and data_frame2. Note that the schema of both the data frames is the same.

Output:

👁 Image

Example 2:

In this example, we have combined two data frames, data_frame1 and data_frame2. Note that the schema of both the data frames is different. Hence, the output is not the desired one as union() function is ideal for datasets having the same structure or schema. 

Output:

👁 Image

UnionAll()  in PySpark

UnionAll() function does the same task as union() function but this function is deprecated since Spark "2.0.0" version. Hence, union() function is recommended.

Syntax:

dataFrame1.unionAll(dataFrame2)

Here,

  • dataFrame1 and dataFrame2 are the dataframes

Example 1:

In this example, we have combined two data frames, data_frame1 and data_frame2. Note that the schema of both the data frames is the same.

Output:

👁 Image

Example 2:

In this example, we have combined two data frames, data_frame1 and data_frame2. Note that the schema of both the data frames is different. Hence, the output is not the desired one as unionAll() function is ideal for datasets having the same structure or schema. 

Output:

👁 Image
Comment
Article Tags: