'Remove all non-matching rows from two differently-sized data frames

I have two dataframes with different row counts.

df1 has the order_id and problems

order_id | problems
123      | broken
342      | torn
593      | wrong_item

df2 has the problems and their count

count    | problems
12       | broken
59       | torn
81       | worn-out

I need to remove all rows from df1 that do not match the problems in df2. I don't really care about the count.

I have tried the following:

df = df1[df1['problems'].isin(df2['problems')]

and

df_matching_rows = pd.merge(df1, df2, how='inner',indicator=True, on='problems')

Both of these do not work. The code runs and gives an output but when I check it, there are problem values in df1 that are present in df2 but not showing. Can someone please help?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source