[英]How to filter row dataframe based on value of another dataframe
How to get filter based data rows from Genre column coming from another dataframe?如何从来自另一个 dataframe 的流派列中获取基于过滤器的数据行?
I have a movies dataframe as follows:我有一部电影 dataframe 如下:
Movie_Name电影名称 | Genre类型 | Rating评分 |
---|---|---|
Halloween万圣节 | Crime, Horror, Thriller犯罪、恐怖、惊悚 | 6.5 6.5 |
Nope没有 | Horror, Mystery, Sci-Fi恐怖、悬疑、科幻 | 6.9 6.9 |
The Midnight Club午夜俱乐部 | Drama, Horror, Mystery剧情、恐怖、悬疑 | 6.7 6.7 |
The Northman北方人 | Action, Adventure, Drama动作, 冒险, 剧情 | 7.1 7.1 |
Prey猎物 | Action, Adventure, Drama动作, 冒险, 剧情 | 7.2 7.2 |
Uncharted神秘海域 | Action, Adventure动作、冒险 | 6.3 6.3 |
Sherwood舍伍德 | Crime, Drama, Mystery犯罪、剧情、悬疑 | 7.4 7.4 |
And I have a user dataframe as follows:我有一个用户 dataframe 如下:
User_Id用户身份 | User_Name用户名 | Genre类型 |
---|---|---|
100 100 | Christine恭 | Horror, Thriller, Drama恐怖、惊悚、剧情 |
I want to get the following rows as output because the user likes horror, thriller, and drama genres.我想获取以下行作为 output,因为用户喜欢恐怖、惊悚和戏剧类型。
Movie_Name电影名称 | Genre类型 | Rating评分 |
---|---|---|
Halloween万圣节 | Crime, Horror, Thriller犯罪、恐怖、惊悚 | 6.5 6.5 |
Nope没有 | Horror, Mystery, Sci-Fi恐怖、悬疑、科幻 | 6.9 6.9 |
The Midnight Club午夜俱乐部 | Drama, Horror, Mystery剧情、恐怖、悬疑 | 6.7 6.7 |
The Northman北方人 | Action, Adventure, Drama动作, 冒险, 剧情 | 7.1 7.1 |
Prey猎物 | Action, Adventure, Drama动作, 冒险, 剧情 | 7.2 7.2 |
Sherwood舍伍德 | Crime, Drama, Mystery犯罪、剧情、悬疑 | 7.4 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?如何获取电影行,其中流派列中的值至少与用户的流派首选项之一匹配?
try this:尝试这个:
pattern = user['Genre'].str.replace(', ', '|')[0]
result = movies.query('Genre.str.contains(@pattern)')
print(result)
The example use a for loop to get a list for each user on df2该示例使用 for 循环获取 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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.