0

How to get filter based data rows from Genre column coming from another dataframe?

I have a movies dataframe as follows:

Movie_Name Genre Rating
Halloween Crime, Horror, Thriller 6.5
Nope Horror, Mystery, Sci-Fi 6.9
The Midnight Club Drama, Horror, Mystery 6.7
The Northman Action, Adventure, Drama 7.1
Prey Action, Adventure, Drama 7.2
Uncharted Action, Adventure 6.3
Sherwood Crime, Drama, Mystery 7.4

And I have a user dataframe as follows:

User_Id User_Name Genre
100 Christine Horror, Thriller, Drama

I want to get the following rows as output because the user likes horror, thriller, and drama genres.

Movie_Name Genre Rating
Halloween Crime, Horror, Thriller 6.5
Nope Horror, Mystery, Sci-Fi 6.9
The Midnight Club Drama, Horror, Mystery 6.7
The Northman Action, Adventure, Drama 7.1
Prey Action, Adventure, Drama 7.2
Sherwood Crime, Drama, Mystery 7.4

How can I get the Movie rows where a value in the Genre column matches at least one of the User's Genre preferences?

2 Answers 2

2

try this:

pattern = user['Genre'].str.replace(', ', '|')[0]
result = movies.query('Genre.str.contains(@pattern)')
print(result)
Sign up to request clarification or add additional context in comments.

Comments

1

The example use a for loop to get a list for each user on df2

import pandas as pd
df=pd.read_csv("db1.csv",header=[0]) # movies
df2=pd.read_csv("db2.csv",header=[0]) # users

for ir,row in df2.iterrows():
    gen=row["Genre"].replace(",","|").replace(" ","")
    filtereddf=df[df["Genre"].str.contains(gen)]
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.