Spark unionAll multiple dataframes

For pyspark you can do the following:

from functools import reduce
from pyspark.sql import DataFrame

dfs = [df1,df2,df3]
df = reduce(DataFrame.unionAll, dfs)

It’s also worth nothing that the order of the columns in the dataframes should be the same for this to work. This can silently give unexpected results if you don’t have the correct column orders!!

If you are using pyspark 2.3 or greater, you can use unionByName so you don’t have to reorder the columns.

Leave a Comment