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

How to find column b when column a equals a certain value in data frames python

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

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

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.

>Solution :

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()
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