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?
CodePudding user response:
try this:
pattern = user['Genre'].str.replace(', ', '|')[0]
result = movies.query('Genre.str.contains(@pattern)')
print(result)
CodePudding user response:
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=print(df[df["Genre"].str.contains(gen)])