0
$\begingroup$

I would like to compare 2 columns in different dataframes, then delete entire non-matching rows.

The 2 columns are not of the same length.

Here is a mock-up of DataFrame #1:

enter image description here

Here is a mock-up of DataFrame #2:

enter image description here

I would like to compare the ID_NUMBER columns in the 2 DataFrames, then delete entire rows in DataFrame #1 if the row value cannot be found in DataFrame #2.

Currently, my code is not working:

DataFrame1 = np.where((DataFrame1["ID_NUMBER"] == DataFrame2["ID_NUMBER"]))

Many thanks in advance!

$\endgroup$

1 Answer 1

1
$\begingroup$

You can use pandas.Series.isin with boolean indexing:

df1[df1["ID_NUMBER"].isin(df2["ID_NUMBER"])]
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.