<?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: pyspark - execute dynamically framed action statement stored in string variable in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/pyspark-execute-dynamically-framed-action-statement-stored-in/m-p/25478#M17718</link>
    <description>&lt;P&gt;Thank you @Shalabh Agarwal​&amp;nbsp;above solution worked for me.&lt;/P&gt;</description>
    <pubDate>Thu, 01 Dec 2022 15:40:52 GMT</pubDate>
    <dc:creator>dineshg</dc:creator>
    <dc:date>2022-12-01T15:40:52Z</dc:date>
    <item>
      <title>pyspark - execute dynamically framed action statement stored in string variable</title>
      <link>https://community.databricks.com/t5/data-engineering/pyspark-execute-dynamically-framed-action-statement-stored-in/m-p/25476#M17716</link>
      <description>&lt;P&gt;I need to execute union statement which is framed dynamically and stored in string variable. I framed the union statement, but struck with executing the statement. Does anyone know how to execute union statement stored in string variable? I'm using pyspark in databricks notebook.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;df1 = df.filter((col("vchDataSection") == "AccountMasterInfo") &amp;amp; (col("bActive") == 1)).withColumn("dfs", concat(lit(".union(df"), col("iRuleid"), lit(")")))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;df2 = df1.agg(concat_ws("",collect_list(col("dfs")))).withColumnRenamed("concat_ws(, collect_list(dfs))", "AccInfoRules").withColumn("replacestr",lit(""))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;df3 = df2.select(overlay("AccInfoRules","replacestr",1,7).alias("overlayed"))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;var_a = df3.collect()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;var_a = var_a[0].__getitem__('overlayed')&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;var_b = var_a.replace(')', '', 1)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;print(var_b)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;o/p: df533.union(df534).union(df535).union(df536)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2022 15:08:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/pyspark-execute-dynamically-framed-action-statement-stored-in/m-p/25476#M17716</guid>
      <dc:creator>dineshg</dc:creator>
      <dc:date>2022-10-27T15:08:56Z</dc:date>
    </item>
    <item>
      <title>Re: pyspark - execute dynamically framed action statement stored in string variable</title>
      <link>https://community.databricks.com/t5/data-engineering/pyspark-execute-dynamically-framed-action-statement-stored-in/m-p/25477#M17717</link>
      <description>&lt;P&gt;@Dineshkumar Gopalakrishnan​&amp;nbsp;using python's exec() function can be used to execute a python statement, which in your case could be pyspark union statement. Refer below sample code snippet for your reference.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;df1 = spark.sparkContext.parallelize([(1, 2, ["1", "2", "3"]), (1, 3, ["4", "1", "5", "6"]) , (2, 4, ["2"]),(2, 5, ["3"])]).toDF(["store", "count", "values"])
&amp;nbsp;
df2 = spark.sparkContext.parallelize([(3, 2, ["1", "2", "3"]), (3, 3, ["4", "1", "5", "6"]) , (4, 4, ["2"]),(4, 5, ["3"])]).toDF(["store", "count", "values"])
&amp;nbsp;
union_statment = "df = df1.union(df2)"
exec(union_statment)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Above code will execute the pyspark union api on df1 and df2 and will assign the result to dataframe 'df'.&lt;/P&gt;&lt;P&gt;You can have more complex union statment as part of your dynamic string&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2022 10:18:49 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/pyspark-execute-dynamically-framed-action-statement-stored-in/m-p/25477#M17717</guid>
      <dc:creator>Shalabh007</dc:creator>
      <dc:date>2022-11-29T10:18:49Z</dc:date>
    </item>
    <item>
      <title>Re: pyspark - execute dynamically framed action statement stored in string variable</title>
      <link>https://community.databricks.com/t5/data-engineering/pyspark-execute-dynamically-framed-action-statement-stored-in/m-p/25478#M17718</link>
      <description>&lt;P&gt;Thank you @Shalabh Agarwal​&amp;nbsp;above solution worked for me.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 15:40:52 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/pyspark-execute-dynamically-framed-action-statement-stored-in/m-p/25478#M17718</guid>
      <dc:creator>dineshg</dc:creator>
      <dc:date>2022-12-01T15:40:52Z</dc:date>
    </item>
    <item>
      <title>Re: pyspark - execute dynamically framed action statement stored in string variable</title>
      <link>https://community.databricks.com/t5/data-engineering/pyspark-execute-dynamically-framed-action-statement-stored-in/m-p/25479#M17719</link>
      <description>&lt;P&gt;amazing @Dineshkumar Gopalakrishnan​&lt;/P&gt;&lt;P&gt;Can you please click on the "&lt;B&gt;Select As Best&lt;/B&gt;" button if you find the information provided helps resolve your question.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 15:46:59 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/pyspark-execute-dynamically-framed-action-statement-stored-in/m-p/25479#M17719</guid>
      <dc:creator>Shalabh007</dc:creator>
      <dc:date>2022-12-01T15:46:59Z</dc:date>
    </item>
  </channel>
</rss>

