<?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 Re: Conditionally create a dataframe in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/conditionally-create-a-dataframe/m-p/11556#M6504</link>
    <description>&lt;P&gt;Probably it could be achieved also in pure SPARK SQL using&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;if(expr1, expr2, expr3)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;so expr1 we check is there rows, expr2 we return union, expr3 we return . Not sure it will work, I can check it later.&lt;/P&gt;</description>
    <pubDate>Thu, 04 Nov 2021 12:12:13 GMT</pubDate>
    <dc:creator>Hubert-Dudek</dc:creator>
    <dc:date>2021-11-04T12:12:13Z</dc:date>
    <item>
      <title>Conditionally create a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/conditionally-create-a-dataframe/m-p/11553#M6501</link>
      <description>&lt;P&gt;I would like to implement a simple logic:&lt;/P&gt;&lt;P&gt;if Df1 is  empty  return Df2 else newDf = Df1.union(Df2)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;May happened that Df1 is empty and the output is simply: []. In that case I do not need union.&lt;/P&gt;&lt;P&gt;I have it like this but getting error when creating dataframe&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;if len(Df1) == 0:
  Df2
else:
  newDf=Df1.union(Df2)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 09:12:27 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/conditionally-create-a-dataframe/m-p/11553#M6501</guid>
      <dc:creator>Braxx</dc:creator>
      <dc:date>2021-11-04T09:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally create a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/conditionally-create-a-dataframe/m-p/11555#M6503</link>
      <description>&lt;P&gt;a dataframe has no 'len' method.  use df.count instead.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That being said: it might be a good idea to always assign the result of your if-else to a dataframe.  Makes it easier to use:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;if df1.count == 0:
  newdf = df2
else:
  newdf = df1.union(df2)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Like that you know the result will always be newdf, instead of df2 or newdf.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 11:50:09 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/conditionally-create-a-dataframe/m-p/11555#M6503</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2021-11-04T11:50:09Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally create a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/conditionally-create-a-dataframe/m-p/11556#M6504</link>
      <description>&lt;P&gt;Probably it could be achieved also in pure SPARK SQL using&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;if(expr1, expr2, expr3)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;so expr1 we check is there rows, expr2 we return union, expr3 we return . Not sure it will work, I can check it later.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 12:12:13 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/conditionally-create-a-dataframe/m-p/11556#M6504</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2021-11-04T12:12:13Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally create a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/conditionally-create-a-dataframe/m-p/11557#M6505</link>
      <description>&lt;P&gt;Also try df.head(1).isEmpty&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 13:12:20 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/conditionally-create-a-dataframe/m-p/11557#M6505</guid>
      <dc:creator>cconnell</dc:creator>
      <dc:date>2021-11-04T13:12:20Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally create a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/conditionally-create-a-dataframe/m-p/11558#M6506</link>
      <description>&lt;P&gt;You were right. Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 16:46:07 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/conditionally-create-a-dataframe/m-p/11558#M6506</guid>
      <dc:creator>Braxx</dc:creator>
      <dc:date>2021-11-04T16:46:07Z</dc:date>
    </item>
  </channel>
</rss>

