Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What would be a Pyspark equivalent of the SQL statement NOT IN

What would be the equivalent code in PySpark?

If I have table A and Table B, and I want to select certain ID from Table A which is not in Table B, I can do the following SQL command:

Select ID
from Table A
where ID not in (Select ID from Table B)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What would be the equivalent code in PySpark?

>Solution :

You could do an "anti-join" with the option "anti":

A_df.show()
# +-----+---+
# | type| id|
# +-----+---+
# |type1| 10|
# |type2| 20|
# +-----+---+


B_df.show()
# +---+-----+----+
# | id| name|type|
# +---+-----+----+
# |  1|name1|  10|
# |  2|name2|  30|
# |  3|name3|  20|
# +---+-----+----+


B_df.join(A_df, B_df.type == A_df.id, "anti").show()
# +---+-----+----+
# | id| name|type|
# +---+-----+----+
# |  2|name2|  30|
# +---+-----+----+

Thsi would be equivalent to select * from B_df where type not in (select id from A_df

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading