2

ok so i used an api and arranged a text file that looks like this:

TITLE,YEAR,IMDB
'Money Plane','2000','tt7286966'
'Mulan','2020','tt4566758'
'Secret Society of Second Born Royals','2020','tt10324122'
'Train to Busan Presents: Peninsula', 'year': '2020', 'imdb_id': 'tt8850222'
'After We Collided','2020','tt10362466'
'One Night in Bangkok','2020','tt12192190'
'Phineas and Ferb The Movie: Candace Against the Universe','2020','tt1817232'
'Cats & Dogs 3: Paws Unite','2020','tt12745164'
'The Crimes That Bind','2020','tt10915060'
'Scoob!','2020','tt3152592'
'We Bare Bears: The Movie','2020','tt10474606'
'Trolls World Tour', 'year': '2020', 'imdb_id': 'tt6587640'
'Birds of Prey (and the Fantabulous Emancipation of One Harley Quinn)','2020','tt7713068'
'Bad Boys for Life','2020','tt1502397'
'Greyhound','2020','tt6048922'
'The Old Guard','2020','tt7556122'
'Sonic the Hedgehog','2020','tt3794354'
'Dad Wanted','2020','tt12721188'
'Barbie: Princess Adventure','2020','tt12767498'
                            

now i want to be able so search a movie by either title,year or id

bassicaly i want to make it so that when i search for a title, it will show me the entire details about the movie, example: search for mulan, and my output will be:

'Mulan','2020','tt4566758'

my code so far looks like this:

import pandas


data=pandas.read_csv(r"C:\Users\Home\Documents\studying\newproject\moviedata.txt")
title=list(data["TITLE"])
year=list(data["YEAR"])
imdbid=list(data["IMDB"])

ive tried to use

for t,y,imdb in zip(title,year,imdbid):
   if word in title:
      items=data[word]
      print(items)

it runs but it does nothing

comingfall
  • 37
  • 6
  • Instead of directly asking what you want, you must show what have you tried and what is the problem in your solution. [Visit Here](https://stackoverflow.com/help/how-to-ask) – JenilDave Oct 04 '20 at 12:52
  • Import the csv to SQLite (https://stackoverflow.com/questions/2887878/importing-a-csv-file-into-a-sqlite3-database-table-using-python) and use SQL to query the data. – balderman Oct 04 '20 at 12:56
  • `df.loc[df['TITLE'].str.contains('your_key_phrase')]` will do just that. I'd recommend learning how indexing and boolean expressions work in pandas it will save you a lot of head ache :) – Umar.H Oct 04 '20 at 13:00
  • ok but now how do i search through the sqlite? – comingfall Oct 04 '20 at 13:19

1 Answers1

1

You can use here numpy.where to find value and pandas loc to print row

Code:

import pandas as pd
import numpy as np

df = pd.read_csv('moviedata.txt')

row , col = np.where(df == "'Mulan'")
print(df.loc[row])

Output:

    TITLE   YEAR    IMDB
1   'Mulan' '2020'  'tt4566758'

If you want output in form of list

row , col = np.where(df == "'Mulan'")
a = df.loc[row]
a = a.values.tolist()
print(a)

Output:

[["'Mulan'", "'2020'", "'tt4566758'"]]
Vishal Upadhyay
  • 781
  • 1
  • 5
  • 19
  • thats really helpfull and i didnt know you could do that with numpy – comingfall Oct 05 '20 at 06:34
  • but when i do the same for the year, i get empty dataframe – comingfall Oct 05 '20 at 06:44
  • @comingfall how are you passing year? Remember your all values are in single inverted commas, this should work : `row , col = np.where(df == "'2020'")` – Vishal Upadhyay Oct 05 '20 at 07:36
  • i managed to fix that by using if statement, now im trying to insert it into a listbox. and inside the listbox its being inserted with the TITLE,YEAR,IMDBID up top. and not as a dataframe – comingfall Oct 05 '20 at 08:08