<?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: I want to compare two data frames. In output I wish to see unmatched Rows and the columns identified leading to the differences. in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/32768#M23901</link>
    <description>&lt;P&gt;Hi @vinita shinde​&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know if It meets your requirement, but you can try something like that&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import pyspark.sql.functions as F
from pyspark.sql import Row
&amp;nbsp;
def is_different(col1, col2, cols):
  if col2 in cols:
    return F.col(col1) == F.col(col2)
  else:
    return F.col(col1).alias(col1)
&amp;nbsp;
def find_difference(data1, data2, id_columns):
  cols = data1.columns
  data_difference = data1.subtract(data2) # First you find the difference in rows
  data_difference = data_difference.join(data2.select(*[F.col(i).alias(f'b_{i}') if i not in id_columns else F.col(i).alias(i) for i in cols]), on = id_columns, how = 'left')
  # you join the differences with the second dataframe
  return data_difference.select(*[is_different(x, f"b_{x}",data_difference.columns) for x in cols]) # then you find out which columns cause the differences
&amp;nbsp;
# df1_spark first dataframe
# df2_spark second datafame
# third option is a list of ids of the Row
display(find_difference(df1_spark, df2_spark, ['ID'] ))
  &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The results will be somethong like that&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/2210i807D65C2408CE93B/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;Let me know if It meets your requirements&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Jan 2022 18:00:58 GMT</pubDate>
    <dc:creator>Pholo</dc:creator>
    <dc:date>2022-01-11T18:00:58Z</dc:date>
    <item>
      <title>I want to compare two data frames. In output I wish to see unmatched Rows and the columns identified leading to the differences.</title>
      <link>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/32764#M23897</link>
      <description />
      <pubDate>Mon, 20 Dec 2021 09:14:14 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/32764#M23897</guid>
      <dc:creator>Databricks_POC</dc:creator>
      <dc:date>2021-12-20T09:14:14Z</dc:date>
    </item>
    <item>
      <title>Re: I want to compare two data frames. In output I wish to see unmatched Rows and the columns identified leading to the differences.</title>
      <link>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/32766#M23899</link>
      <description>&lt;P&gt;Are you trying to do change data capture between an older and newer version of the same table?&lt;/P&gt;</description>
      <pubDate>Mon, 20 Dec 2021 19:26:51 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/32766#M23899</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-12-20T19:26:51Z</dc:date>
    </item>
    <item>
      <title>Re: I want to compare two data frames. In output I wish to see unmatched Rows and the columns identified leading to the differences.</title>
      <link>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/32767#M23900</link>
      <description>&lt;P&gt;No, I wish to compare two tables. Same Scenario as in case of Minus/Except query. However apart from the mismatched rows, I wish to also know which are those columns leading to the difference. &lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 08:59:42 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/32767#M23900</guid>
      <dc:creator>Databricks_POC</dc:creator>
      <dc:date>2021-12-21T08:59:42Z</dc:date>
    </item>
    <item>
      <title>Re: I want to compare two data frames. In output I wish to see unmatched Rows and the columns identified leading to the differences.</title>
      <link>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/32768#M23901</link>
      <description>&lt;P&gt;Hi @vinita shinde​&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know if It meets your requirement, but you can try something like that&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import pyspark.sql.functions as F
from pyspark.sql import Row
&amp;nbsp;
def is_different(col1, col2, cols):
  if col2 in cols:
    return F.col(col1) == F.col(col2)
  else:
    return F.col(col1).alias(col1)
&amp;nbsp;
def find_difference(data1, data2, id_columns):
  cols = data1.columns
  data_difference = data1.subtract(data2) # First you find the difference in rows
  data_difference = data_difference.join(data2.select(*[F.col(i).alias(f'b_{i}') if i not in id_columns else F.col(i).alias(i) for i in cols]), on = id_columns, how = 'left')
  # you join the differences with the second dataframe
  return data_difference.select(*[is_different(x, f"b_{x}",data_difference.columns) for x in cols]) # then you find out which columns cause the differences
&amp;nbsp;
# df1_spark first dataframe
# df2_spark second datafame
# third option is a list of ids of the Row
display(find_difference(df1_spark, df2_spark, ['ID'] ))
  &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The results will be somethong like that&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/2210i807D65C2408CE93B/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;Let me know if It meets your requirements&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jan 2022 18:00:58 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/32768#M23901</guid>
      <dc:creator>Pholo</dc:creator>
      <dc:date>2022-01-11T18:00:58Z</dc:date>
    </item>
    <item>
      <title>Re: I want to compare two data frames. In output I wish to see unmatched Rows and the columns identified leading to the differences.</title>
      <link>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/32769#M23902</link>
      <description>&lt;P&gt;@vinita shinde​&amp;nbsp;are you Cracked this Code?&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2022 08:53:19 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/32769#M23902</guid>
      <dc:creator>bhargavi1</dc:creator>
      <dc:date>2022-04-28T08:53:19Z</dc:date>
    </item>
    <item>
      <title>Re: I want to compare two data frames. In output I wish to see unmatched Rows and the columns identi</title>
      <link>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/55757#M30426</link>
      <description>&lt;P&gt;How to print only what columns changed instead of displaying all columns?&lt;/P&gt;&lt;P&gt;I just want to print for every row PRIMARY Key (ID) and only column names that changed in the second data frame as comma separated.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Dec 2023 18:32:28 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/i-want-to-compare-two-data-frames-in-output-i-wish-to-see/m-p/55757#M30426</guid>
      <dc:creator>Enthusiastic_Da</dc:creator>
      <dc:date>2023-12-26T18:32:28Z</dc:date>
    </item>
  </channel>
</rss>

