0

I have two data frames, take the example below

df1

userid| name  
    33|  Paul   
    2 |  jack   
    32|  Ryan   

df2

userid| sport | song 
79 |  tennis  | lovelies  
33 |  swimmin | Beatles  
21 |  boxing  | stones  

how would I get a df3, where if the userids match between df1, and df2, I want to df3 to contain only those rows from df2 where they matched with df1

I don't think a JOIN is the answer here, because I want ONLY the rows from df2. not the rows from df1.

Thanks!

SteelFox
  • 93
  • 1
  • 12

1 Answers1

1

use pandas.Series.isin

df2[df2['userid'].isin(df1['userid'])]

output

   userid      sport        song 
1      33    swimmin    Beatles  
Yuca
  • 6,010
  • 3
  • 22
  • 42
  • I don't want the name from df1. I just want all the rows from df2. – SteelFox Oct 01 '18 at 17:37
  • thanks a lot this helps a lot. just a clarification, if I have user ids in 2 different columns in df1, would df2[df2['userid'].isin(df1['userid1']).isin(df1['userid2']) work? – SteelFox Oct 01 '18 at 17:47
  • it all depends on what you want, you're doing an `and` operation there, if you want that, then that code is correct – Yuca Oct 01 '18 at 17:50
  • this answer didn't actually work for me, since it only made more columns, I dint get a data frame. the resulting data frame had 0 rows, and matching values were made as columns – SteelFox Oct 01 '18 at 17:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181100/discussion-between-yuca-and-steelfox). – Yuca Oct 01 '18 at 18:00