gpt4 book ai didi

python - 如何显示与输入匹配的同一行的值?

转载 作者:行者123 更新时间:2023-12-02 02:38:58 25 4
gpt4 key购买 nike

数据有 2 列,分别是 titlegenre。所以我试图给 title 与用户输入类型匹配的行的值。

这是我的尝试:

#CSV READ & GENRE-TITLE
data = pd.read_csv("data.csv")
df_title = data["title"]
df_genre = data["genre"]

#TOKENIZE
tokenized_genre = [word_tokenize(i) for i in df_genre]
tokenized_title = [word_tokenize(i) for i in df_title]

#INPUT-DATA MATCH
search = {e.lower() for l in tokenized_genre for e in l}
choice = input('Please enter a word = ')

while choice != "exit":
if choice.lower() in search:
print(data.loc[data.genre == {choice}, 'title'])
else:
print("The movie of the genre doesn't exist")
choice = input("Please enter a word = ")

但结果是:Series([], Name: title, dtype: object)

我该如何解决?

编辑:标题的数据样本

0                              The Story of the Kelly Gang
1 Den sorte drøm
2 Cleopatra
3 L'Inferno
4 From the Manger to the Cross; or, Jesus of
...

对于流派:

0          Biography, Crime, Drama
1 Drama
2 Drama, History
3 Adventure, Drama, Fantasy
4 Biography, Drama
...

最佳答案

一个提案仅基于Pandas

我会建议这样的事情(请根据你的意愿适应你的情况,这只是一些一般的指导方针和提示,你可以从哪里开始):

import pandas as pd

# Warning: there are coma and semi-column in some of the films titles,
# so I had to use an other separator when exporting data to CSV,
# here I decided to chose the vertical bar '|' as you can see)

#CSV READ & GENRE-TITLE
data = pd.read_csv("data.csv", sep="|")

choice = input('Please enter a word = ')

while choice != "exit":
choice = choice.lower()
for index, row in data.iterrows():
if choice in row['genre'].lower():
print(row['title'])
else:
print(("The movie of the genre {} doesn't exist").format(choice))
choice = input("Please enter a word = ")


编辑

生成随机数:

from random import randint
i = randint(0, len(data))

然后,使用 i 作为索引在您的 DataFrame 中搜索。
我让你玩这个。



有用的链接

Does Python have a string 'contains' substring method?
How to iterate over rows in a DataFrame in Pandas?

关于python - 如何显示与输入匹配的同一行的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61026669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com