How to filter row dataframe based on value of another dataframe

Question:

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

I have dataframe movies 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

I have dataframe user as follows:

User_Id User_Name Genre
100 Christine Horror, Thriller, Drama

Output that i want like this :

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

Because user like horror, thriller, and drama genres. How to make the code to get the output like above?

Asked By: Annisa Lianda

||

Answers:

try this:

pattern = user['Genre'].str.replace(', ', '|')[0]
result = movies.query('Genre.str.contains(@pattern)')
print(result)
Answered By: ziying35

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)]
    
Answered By: wrbp
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.