如何显示与输入匹配的同一行的值?
How to display the value that in the same row which matched with the input?
数据有 2 列,即 title
和 genre
。所以我试图给与用户输入类型匹配的行的 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?
数据有 2 列,即 title
和 genre
。所以我试图给与用户输入类型匹配的行的 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?