I have a Pandas DataFrame structured like this:
user_id movie_id rating
0 1 1193 5
1 2 1193 5
2 12 1193 4
3 15 1193 4
4 17 1193 5
5 18 1193 4
6 19 1193 5
7 24 1193 5
8 28 1193 3
Each row corresponds to a rating
event performed by the user user_id
for the movie movie_id
. For instance, the first row says that user 1
rated the movie 1193
with a rating of 5
.
This data comes from the MovieLens project.
My goal is to find all the users who satisfy these two conditions:
- rated movie
588
with a rating of5
- rated movie
3578
with a rating of3
I came up with two filtered DataFrame objects for each of the above conditions:
ratings_588_5 = data[(data.movie_id == 588) & (data.rating == 5]
ratings_3578_3 = data[(data.movie_id == 3578) & (data.rating == 3)]
Which result in, respectively:
>>> ratings_588_5
user_id movie_id rating
438 588 5
758 588 5
913 588 5
1024 588 5
1214 588 5
>>> ratings_3578_3
user_id movie_id rating
45 3578 3
321 3578 3
467 3578 3
758 3578 3
1024 3578 3
1381 3578 3
In Pandas, how can I compute a list
of all user_id
which appear in both DataFrames?
In this example, the result I want to obtain is:
[758, 1024]