![]() |
VOOZH | about |
In this article, we will discuss Union and UnionAll in PySpark in Python.
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:
👁 ImageExample 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:
👁 ImageUnionAll() 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:
👁 ImageExample 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.