<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to union multiple dataframe in pyspark within Databricks notebook in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-union-multiple-dataframe-in-pyspark-within-databricks/m-p/27305#M19182</link>
    <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I have 4 DFs: Avg_&lt;B&gt;Open&lt;/B&gt;&lt;I&gt;By_Year, Avg&lt;/I&gt;&lt;B&gt;High&lt;/B&gt;&lt;I&gt;By_Year, Avg&lt;/I&gt;&lt;B&gt;Low&lt;/B&gt;&lt;I&gt;By_Year and Avg&lt;/I&gt;&lt;B&gt;Close&lt;/B&gt;_By_Year, all of them have a common column of '&lt;B&gt;Year&lt;/B&gt;'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;I want to join the three together to get a final df like:&lt;P&gt;&lt;/P&gt;`Year, Open, High, Low, Close`&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;At the moment I have to use the ugly way to join them on column 'Year':
&lt;PRE&gt;&lt;CODE&gt;finalDF = Avg_Open_By_Year
.join(Avg_High_By_Year, on=['Year'], how='left_outer')
.join(Avg_Low_By_Year, on=['Year'], how='left_outer')
.join(Avg_Close_By_Year, on=['Year'], how='left_outer')
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;I think there should be a grace way to accomplish this, like UnionAll in SQL.&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;There is a possible solution here &lt;A href="https://datascience.stackexchange.com/questions/11356/merging-multiple-data-frames-row-wise-in-pyspark/11361#11361" target="test_blank"&gt;https://datascience.stackexchange.com/questions/11356/merging-multiple-data-frames-row-wise-in-pyspark/11361#11361&lt;/A&gt;, the selected answer is described below:from functools import reduce # For Python 3.x from pyspark.sql import DataFrame
&lt;P&gt;&lt;/P&gt; 
&lt;P&gt; def unionAll(*dfs): return reduce(DataFrame.unionAll, dfs)&lt;/P&gt; 
&lt;P&gt; unionAll(td2, td3, td4, td5, td6, td7, td8, td9, td10) &lt;/P&gt;
&lt;P&gt;However, I am doing this in Databricks notebook, it throws me error:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt; NameError: name 'functools' is not defined
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="0693f000007OoI6AAK"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2502iB90ABDD4968A8471/image-size/large?v=v2&amp;amp;px=999" role="button" title="0693f000007OoI6AAK" alt="0693f000007OoI6AAK" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;It would really be appreciated if someone can shed me with more light. Thank you very much. 
&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 31 Jan 2020 04:14:18 GMT</pubDate>
    <dc:creator>RaymondXie</dc:creator>
    <dc:date>2020-01-31T04:14:18Z</dc:date>
    <item>
      <title>How to union multiple dataframe in pyspark within Databricks notebook</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-union-multiple-dataframe-in-pyspark-within-databricks/m-p/27305#M19182</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I have 4 DFs: Avg_&lt;B&gt;Open&lt;/B&gt;&lt;I&gt;By_Year, Avg&lt;/I&gt;&lt;B&gt;High&lt;/B&gt;&lt;I&gt;By_Year, Avg&lt;/I&gt;&lt;B&gt;Low&lt;/B&gt;&lt;I&gt;By_Year and Avg&lt;/I&gt;&lt;B&gt;Close&lt;/B&gt;_By_Year, all of them have a common column of '&lt;B&gt;Year&lt;/B&gt;'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;I want to join the three together to get a final df like:&lt;P&gt;&lt;/P&gt;`Year, Open, High, Low, Close`&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;At the moment I have to use the ugly way to join them on column 'Year':
&lt;PRE&gt;&lt;CODE&gt;finalDF = Avg_Open_By_Year
.join(Avg_High_By_Year, on=['Year'], how='left_outer')
.join(Avg_Low_By_Year, on=['Year'], how='left_outer')
.join(Avg_Close_By_Year, on=['Year'], how='left_outer')
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;I think there should be a grace way to accomplish this, like UnionAll in SQL.&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;There is a possible solution here &lt;A href="https://datascience.stackexchange.com/questions/11356/merging-multiple-data-frames-row-wise-in-pyspark/11361#11361" target="test_blank"&gt;https://datascience.stackexchange.com/questions/11356/merging-multiple-data-frames-row-wise-in-pyspark/11361#11361&lt;/A&gt;, the selected answer is described below:from functools import reduce # For Python 3.x from pyspark.sql import DataFrame
&lt;P&gt;&lt;/P&gt; 
&lt;P&gt; def unionAll(*dfs): return reduce(DataFrame.unionAll, dfs)&lt;/P&gt; 
&lt;P&gt; unionAll(td2, td3, td4, td5, td6, td7, td8, td9, td10) &lt;/P&gt;
&lt;P&gt;However, I am doing this in Databricks notebook, it throws me error:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt; NameError: name 'functools' is not defined
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="0693f000007OoI6AAK"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2502iB90ABDD4968A8471/image-size/large?v=v2&amp;amp;px=999" role="button" title="0693f000007OoI6AAK" alt="0693f000007OoI6AAK" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;It would really be appreciated if someone can shed me with more light. Thank you very much. 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2020 04:14:18 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-union-multiple-dataframe-in-pyspark-within-databricks/m-p/27305#M19182</guid>
      <dc:creator>RaymondXie</dc:creator>
      <dc:date>2020-01-31T04:14:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to union multiple dataframe in pyspark within Databricks notebook</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-union-multiple-dataframe-in-pyspark-within-databricks/m-p/27306#M19183</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Import reduce function in this way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;from functools import reduce&lt;/CODE&gt;&lt;/PRE&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Feb 2021 15:11:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-union-multiple-dataframe-in-pyspark-within-databricks/m-p/27306#M19183</guid>
      <dc:creator>thiago_matos</dc:creator>
      <dc:date>2021-02-04T15:11:38Z</dc:date>
    </item>
  </channel>
</rss>

