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

Efficiently eliminating rows in a dataframe based on values in another dataframe

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?

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

>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

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