<?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 create UDF in pyspark in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/create-udf-in-pyspark/m-p/38577#M5577</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Need the help of this community, unfortunately creating udfs is not my strongest skill set.&lt;/P&gt;&lt;P&gt;I need to create UDF that will join two tables together, the problem is that one table has two id columns&amp;nbsp;&lt;/P&gt;&lt;P&gt;Name Table has id1 and id2&amp;nbsp;&lt;/P&gt;&lt;P&gt;Transaction Table has only id&lt;/P&gt;&lt;P&gt;I need to join Transaction table with id table. the constraint is if id2 in name table populated then join with id2 else join with id1&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried some things but none of them is 100% correct.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="LiliL_0-1690464091302.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/3004i6A98CD6CA39AC328/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="LiliL_0-1690464091302.png" alt="LiliL_0-1690464091302.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="LiliL_1-1690464169973.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/3005iDA6E6B56F5ABABE5/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="LiliL_1-1690464169973.png" alt="LiliL_1-1690464169973.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Jul 2023 13:25:04 GMT</pubDate>
    <dc:creator>LiliL</dc:creator>
    <dc:date>2023-07-27T13:25:04Z</dc:date>
    <item>
      <title>create UDF in pyspark</title>
      <link>https://community.databricks.com/t5/get-started-discussions/create-udf-in-pyspark/m-p/38577#M5577</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Need the help of this community, unfortunately creating udfs is not my strongest skill set.&lt;/P&gt;&lt;P&gt;I need to create UDF that will join two tables together, the problem is that one table has two id columns&amp;nbsp;&lt;/P&gt;&lt;P&gt;Name Table has id1 and id2&amp;nbsp;&lt;/P&gt;&lt;P&gt;Transaction Table has only id&lt;/P&gt;&lt;P&gt;I need to join Transaction table with id table. the constraint is if id2 in name table populated then join with id2 else join with id1&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried some things but none of them is 100% correct.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="LiliL_0-1690464091302.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/3004i6A98CD6CA39AC328/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="LiliL_0-1690464091302.png" alt="LiliL_0-1690464091302.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="LiliL_1-1690464169973.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/3005iDA6E6B56F5ABABE5/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="LiliL_1-1690464169973.png" alt="LiliL_1-1690464169973.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 13:25:04 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/create-udf-in-pyspark/m-p/38577#M5577</guid>
      <dc:creator>LiliL</dc:creator>
      <dc:date>2023-07-27T13:25:04Z</dc:date>
    </item>
    <item>
      <title>Re: create UDF in pyspark</title>
      <link>https://community.databricks.com/t5/get-started-discussions/create-udf-in-pyspark/m-p/38860#M5578</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I am not sure if I understand your question directly but let me give it a try:&lt;BR /&gt;- &lt;STRONG&gt;T&lt;/STRONG&gt;&lt;SPAN&gt;&lt;STRONG&gt;he constraint is if id2 in name table populated then join with id2:&lt;/STRONG&gt; So I think you can also could first make a column called 'id' in which you get id2 if it is populated and id1 if it is not populated like:&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pyspark.sql import functions as F

name_table = name_table.withColumn('id', F.when(F.col('id2').notnull(), F.col('id2')).otherwise(F.col('id1'))&lt;/LI-CODE&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;- I need to join Transaction table with id table.&amp;nbsp;&lt;/STRONG&gt;&lt;BR /&gt;After creating this, you can use this new id to join the names on the transaction table:&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;transaction_table = transaction_table.join(name_table, on=['id'],how='inner')&lt;/LI-CODE&gt;&lt;P&gt;&lt;SPAN&gt;So expect that you would to do a inner join here, but it could also be a different join. More info is here:&amp;nbsp;&lt;A href="https://spark.apache.org/docs/3.1.2/api/python/reference/api/pyspark.sql.DataFrame.join.html" target="_blank"&gt;https://spark.apache.org/docs/3.1.2/api/python/reference/api/pyspark.sql.DataFrame.join.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Furthermore, if the name_table is a small table then it is suggest to use F.broadcast to make the join faster/smoother:&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pyspark.sql import functions as F
transaction_table = transaction_table.join(F.broadcast(name_table), on=['id'],how='inner')&lt;/LI-CODE&gt;&lt;P&gt;More information is here:&amp;nbsp;&lt;A href="https://spark.apache.org/docs/3.1.1/api/python/reference/api/pyspark.Broadcast.html" target="_blank"&gt;https://spark.apache.org/docs/3.1.1/api/python/reference/api/pyspark.Broadcast.html&lt;/A&gt;.&lt;BR /&gt;&lt;BR /&gt;I hope this helps a bit and else please ask further.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2023 18:24:35 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/create-udf-in-pyspark/m-p/38860#M5578</guid>
      <dc:creator>Siebert_Looije</dc:creator>
      <dc:date>2023-08-01T18:24:35Z</dc:date>
    </item>
  </channel>
</rss>

