# Union and UnionAll – Merge DataFrames in PySpark

## Union and UnionAll-

In PySpark Union() and UnionAll() is used to merge or concatenate two or more dataframes. To merge two dataframes, you must be sure that they have the same schema and number of columns otherwise the union will fail.

Let’s create two dataframe to work with.

#### First DataFrame –

``````sampleData1 = [
('Eleven', 18, 'F', 99),
('Mike', 20, 'M', 85),
('Lucas', 20, 'M', 82),
('Will', 18, 'M', 70),
('Max', 19, 'F', 80)
]

columns1 = ['Name', 'Age', 'Sex', 'Marks']

df1 = spark.createDataFrame(data= sampleData1, schema= columns1)
df1.show()``````

#### Second dataframe –

``````sampleData2 = [
('Eleven', 18, 'F', 99),
('Dustin', 17, 'M', 70),
('Steve', 20, 'M', 80),
('Nancy', 20, 'F', 75),
('Mike', 20, 'M', 85)
]

columns2 = ['Name', 'Age', 'Sex', "Marks"]

df2 = spark.createDataFrame(data= sampleData2, schema= columns2)
df2.show()``````

## Merge two or more dataframes using Union –

The union() method in PySpark merge two dataframes and returns a new dataframe with all the rows from both the dataframe including any duplicate records.

Let’s merge the df1 and df2.

``````df3 = df1.union(df2)
df3.show()``````

## Merge two or more dataframes using unionAll –

UnionAll() method also return the same result as above and it has been deprecated since PySpark “2.0.0” version and recommends using the union() method.

``````df4 = df1.unionAll(df2)
df4.show()``````

## Merge dataframes without duplicates –

To merge dataframes without duplicates, first we use the union() method to combine two dataframes and then use the distinct() method to remove any duplicate records.

``````df5 = df1.union(df2).distinct()
df5.show()``````

Rating: 1 out of 5.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5899781,"math_prob":0.95530677,"size":1562,"snap":"2022-27-2022-33","text_gpt3_token_len":454,"char_repetition_ratio":0.14313222,"word_repetition_ratio":0.033613447,"special_character_ratio":0.33354673,"punctuation_ratio":0.21794872,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98654896,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-09T09:51:53Z\",\"WARC-Record-ID\":\"<urn:uuid:d0294128-1c59-42ec-b5cd-d24c7db765db>\",\"Content-Length\":\"181864\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a542d210-86e6-40d4-81d9-c35f5f277849>\",\"WARC-Concurrent-To\":\"<urn:uuid:5f21acb8-fa08-4de8-8210-f11cc343e1a6>\",\"WARC-IP-Address\":\"192.0.78.249\",\"WARC-Target-URI\":\"https://lifewithdata.com/2022/07/29/union-and-unionall-merge-dataframes-in-pyspark/\",\"WARC-Payload-Digest\":\"sha1:YIWHE6JYQYRLVOI5SHWCVR2EL7L7GLPT\",\"WARC-Block-Digest\":\"sha1:IET3MBTXE3SSP2JDTL4ZC3PMCURD2WSE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882570921.9_warc_CC-MAIN-20220809094531-20220809124531-00145.warc.gz\"}"}