I am looking for a way to find difference in values, in columns of two DataFrame. For example:
from pyspark.sql import SQLContext
sc = SparkContext()
sql_context = SQLContext(sc)
df_a = sql_context.createDataFrame([("a", 3), ("b", 5), ("c", 7)], ["name", "id"])
df_b = sql_context.createDataFrame([("a", 3), ("b", 10), ("c", 13)], ["name", "id"])
DataFrame A:
+----+---+
|name| id|
+----+---+
| a| 3|
| b| 5|
| c| 7|
+----+---+
DataFrame B:
+----+---+
|name| id|
+----+---+
| a| 3|
| b| 10|
| c| 13|
+----+---+
My goal is a list
of id
column elements that are in A but not in B, e.g: [5, 7]
. I was thinking of doing a join on id
, but I don't see a good way to do it.
Naive solution could be:
list_a = df_a.select("id").rdd.map(lambda x: x.asDict()["id"]).collect()
list_b = df_b.select("id").rdd.map(lambda x: x.asDict()["id"]).collect()
result = list(set(list_a).difference(list_b))
But, is there a simple solution that can be obtained with just DataFrame operations, except perhaps the final collect?
Use the subtract
function
df_a.select('id').subtract(df_b.select('id')).collect()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With