You can solve this problem by joining the two DataFrames on the unique ID column and then comparing each column value by value. One way to do this is to:
Identify the Columns to Compare:
Exclude the unique ID column from the list of columns you want to compare.
Join the DataFrames:
Join the two DataFrames on the unique ID column. To keep things clear, alias the DataFrames (for example, as df1 and df2).
Compare Columns:
For each column you wish to compare, add a new column (or multiple columns) to show where differences occur. You can use a when clause from PySpark's functions to flag differences. One option is to create a new column that, for each compared column, shows both values if they differ or null if they match.
Display or Further Process the Differences:
Once you have your comparison result, you can inspect which columns (and rows) differ.
Below is a complete example:
python
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, when, lit, concat_ws
# Start Spark session
spark = SparkSession.builder.appName("CompareDataFrames").getOrCreate()
# Example DataFrames (suppose both have same schema including 'id' as unique identifier)
data1 = [
(1, "Alice", 30),
(2, "Bob", 25),
(3, "Charlie", 40)
]
data2 = [
(1, "Alice", 32), # age differs for id 1
(2, "Bobby", 25), # name differs for id 2
(3, "Charlie", 40)
]
columns = ["id", "name", "age"]
df1 = spark.createDataFrame(data1, columns)
df2 = spark.createDataFrame(data2, columns)
# List of columns to compare (excluding the unique id)
compare_columns = [c for c in df1.columns if c != "id"]
# Join the two DataFrames on 'id'. Use aliases to differentiate columns.
df_join = df1.alias("df1").join(df2.alias("df2"), on="id")
# Function to create a comparison column for each column
def compare_column(col_name):
return when(
col(f"df1.{col_name}") != col(f"df2.{col_name}"),
# If they differ, show both values (you can customize the output here)
concat_ws(" -> ", col(f"df1.{col_name}").cast("string"), col(f"df2.{col_name}").cast("string"))
).otherwise(lit(None)).alias(col_name + "_diff")
# Create comparison columns for each column in compare_columns
diff_cols = [compare_column(c) for c in compare_columns]
# Select the unique id and each difference column
df_diff = df_join.select("id", *diff_cols)
# Display the result
df_diff.show(truncate=False)
Explanation of the Code:
DataFrames Creation:
Two example DataFrames (df1 and df2) are created. They both have an "id" column as the unique identifier.
Joining:
We alias each DataFrame as "df1" and "df2" and join them on the "id" column. This way we can explicitly refer to columns from each DataFrame.
Comparing Columns:
For each column (except "id"), the compare_column function generates a new column named column_diff. It uses a when clause to check if the values in df1 and df2 are different. If they are, it concatenates the two differing values with an arrow (->) in between; otherwise, it returns null.
Result:
The final DataFrame df_diff will have the unique "id" and a set of columns (one per compared column) that indicate the differences. For the sample data, you might see:
This is one efficient way to highlight where the differences exist between the two PySpark DataFrames. You can further customize the logic based on your specific needs (for example, handling nulls differently or summarizing differences in another format).