<?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: Comparing 2 dataframes and create columns from values within a dataframe in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/comparing-2-dataframes-and-create-columns-from-values-within-a/m-p/12531#M7331</link>
    <description>&lt;P&gt;You need to join and pivot&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;df
.join(df2, on=[df.company == df2.job_company]))
.groupBy("company", "name")
.pivot("job_company")
.count()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image.png"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/857i18220738C727E685/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 11 Jan 2023 16:59:13 GMT</pubDate>
    <dc:creator>Hubert-Dudek</dc:creator>
    <dc:date>2023-01-11T16:59:13Z</dc:date>
    <item>
      <title>Comparing 2 dataframes and create columns from values within a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/comparing-2-dataframes-and-create-columns-from-values-within-a/m-p/12530#M7330</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a dataframe that has name and company&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;from pyspark.sql import SparkSession&lt;/P&gt;&lt;P&gt;spark = SparkSession.builder.appName('&lt;A href="https://SparkByExamples.com" alt="https://SparkByExamples.com" target="_blank"&gt;SparkByExamples.com&lt;/A&gt;').getOrCreate()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;columns = ["company","name"]&lt;/P&gt;&lt;P&gt;data = [("company1", "Jon"), ("company2", "Steve"), ("company1", "Kim"), ("company3", "Sam"), ("company4", "Jim"), ("company4", "Tony"), ("company5", "Stan"),&lt;/P&gt;&lt;P&gt;]&lt;/P&gt;&lt;P&gt;df = spark.createDataFrame(data=data, schema = columns).show()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then I have another dataframe that has the company names &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;columns2 = ["job_comany","num"]&lt;/P&gt;&lt;P&gt;data2 = [("company1",1), ("company2",2), ("company3",3), ("company4",4), ("company5",5),]&lt;/P&gt;&lt;P&gt;df2 = spark.createDataFrame(data=data2, schema = columns2).show()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I would like to do is use the company names dataframe to search the dataframe with the person names and identify the companies associated with the people and create a dataframe with the company names as columns with a 0 or 1 with if the person is with that company. Here is a picture of what I would like to see as my final dataframe.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/856i3B4B8468B948E6E5/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jan 2023 16:08:37 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/comparing-2-dataframes-and-create-columns-from-values-within-a/m-p/12530#M7330</guid>
      <dc:creator>lmcglone</dc:creator>
      <dc:date>2023-01-11T16:08:37Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing 2 dataframes and create columns from values within a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/comparing-2-dataframes-and-create-columns-from-values-within-a/m-p/12531#M7331</link>
      <description>&lt;P&gt;You need to join and pivot&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;df
.join(df2, on=[df.company == df2.job_company]))
.groupBy("company", "name")
.pivot("job_company")
.count()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image.png"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/857i18220738C727E685/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jan 2023 16:59:13 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/comparing-2-dataframes-and-create-columns-from-values-within-a/m-p/12531#M7331</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2023-01-11T16:59:13Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing 2 dataframes and create columns from values within a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/comparing-2-dataframes-and-create-columns-from-values-within-a/m-p/12532#M7332</link>
      <description>&lt;P&gt;Thanks....that is perfect. &lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another question to take it a step further. From this code how would I alter the name of the produced column name. In your example there is company1, company2, etc. Is it possible to change those names to company1_a, company2_a, etc.?&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jan 2023 19:47:14 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/comparing-2-dataframes-and-create-columns-from-values-within-a/m-p/12532#M7332</guid>
      <dc:creator>lmcglone</dc:creator>
      <dc:date>2023-01-11T19:47:14Z</dc:date>
    </item>
  </channel>
</rss>

