My dataframes:
df_1 =
userId itemId rating
a i1 0
b i2 1
b i3 0
b i4 1
c i4 1
df_2 =
itemId info1 info2
i1 30 0
i2 20 1
i3 20 0
I want to eliminate the rows in the first dataframe that contain itemIds which don’t appear in the second dataframe, so in this case, i4 doesn’t appear so my new dataframe would be:
userId itemId rating
a i1 0
b i2 1
b i3 0
What I tried: I merged the two and then deleted the columns info1 and info2. I find it unsafe because my dataframes are huge and the same itemId appears several times in df_1. Is there a better way to do it?
>Solution :
This will do the job:
df_1[df_1['itemId'].isin(df_2['itemId'])]
Explanation:
df_1['itemId'].isin(df_2['itemId']) will generate a boolean based on if an itemId is present in df_2 or not and then we will pass that boolean to the df_1 to get the entries in df_1 with itemId present in df_2