简体   繁体   English

如何根据另一个 dataframe 的值过滤行 dataframe

[英]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.

相关问题 如何根据一行是否包含另一行中的值组合数据框中的行 - How to combine rows in dataframe based on if a row contains a value in another row 根据来自另一个数据帧的列值在数据帧中查找一行并对其应用过滤器 - Find a row in a dataframe based on a column value from another dataframe and apply filter to it 您如何根据另一个 dataframe 中列的值以及该 Z6A8064B5DF479455500553C47C5505234067B 中的列字符串是否为 ZE8064B5DF47C55057DZ 过滤 dataframe? - How do you filter dataframe based off value of column in another dataframe and whether the string of a column in that dataframe is a substring? 数据框的基于行的过滤器 - Row based Filter of a dataframe 如何根据行中的另一个值在 dataframe 中创建列(Python) - How to create a column in a dataframe based on another value in the row (Python) 我如何根据列单元格值和 append 查找一个 dataframe 上的一行到另一个 dataframe 上的一行? - How do i lookup a row on one dataframe based on the column cell value and append that to a row on another dataframe? 如何将基于列的 dataframe 中的值添加到基于行的另一个 dataframe 中? - How do I add the value from one dataframe based on a column to another dataframe based on a row? 如何根据另一个 dataframe 的行名删除 dataframe 熊猫中的行? - How to delete the row in a dataframe panda based on the row names of another dataframe? 基于另一个 dataframe 的行值对一个 dataframe 中的列求和 - Sum column in one dataframe based on row value of another dataframe 如何根据 python 中另一个 dataframe 的值过滤 dataframe - How to filter a dataframe based on the values of another dataframe in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM