I have a csv that contains user Ids movie Ids and ratings and I would like to return all of the movie ids for certain user ids. This is what the data looks like
userId movieId rating timestamp
1 1 4.0 964982703
1 3 4.0 964981247
1 6 4.0 964982224
1 47 5.0 964983815
1 50 5.0 964982931
say I want to return all of the movies for user id 1 for example and store them in some sort of structure maybe a list. The output would be [1,3,6,47,50, … ].
I tried using this code just to print the outputs
for x in ratings["userId"]:
for y in ratings["movieId"]:
if x == 1:
print(y)
but my output would just keep printing forever and forced vs code to shut down.
CodePudding user response:
You should loop on the same row:
for x in zip(ratings["userId"], ratings["movieId"]):
if x == 1:
print(y)
Or
res = ratings.loc[ratings["userId"].eq(1),"movieId"].tolist()
CodePudding user response:
assuming userId
is a string column, you can run:
print(ratings.query("userId == '1'")["movieId"])
otherwise, if it's an integer column, you can run:
print(ratings.query("userId == 1")["movieId"])