Home > Back-end >  Getting 'KeyError' while trying to get matching values with two csv files
Getting 'KeyError' while trying to get matching values with two csv files

Time:05-29

So I just tried making a basic 10 line movie recommendation system with a big ML project in mind. But It's only errors I'm getting while running this:

import pandas as pd

movies = pd.read_csv('movies.csv')
users = pd.read_csv('users.csv')

recommendations = {}
def recommend(users,movies):
    for f in users['favouritegenre']:
            genre = movies.query(f)['genre']
            movie = movies.query(genre)['movie']
            userid = users.query(f)['userid']
            recommendations[userid]=movie
    print(recommendations)

recommend(users,movies)

My movies csv file:

movie,ratings,genre
'toy story','8','family'
'john wick','7','action'
'a quite place','8','horror'

users csv file:

userid,age,favouritegenre
'1','16','family'
'2','49','action'
'3','10','horror'

and I get the error:

Traceback (most recent call last):
  File "main.py", line 15, in <module>
    recommend(users,movies)
  File "main.py", line 9, in recommend
    genre = movies.query(f)['genre']
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\frame.py", line 4114, in query
    result = self.loc[res]
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 967, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1202, in _getitem_axis
    return self._get_label(key, axis=axis)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1153, in _get_label
    return self.obj.xs(label, axis=axis)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\generic.py", line 3864, in xs
    loc = index.get_loc(key)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexes\range.py", line 389, in get_loc
    raise KeyError(key)
KeyError: 'family'

CodePudding user response:

IIUC, you can do

recommendations = (users.assign(movies=users['favouritegenre'].map(movies.groupby('genre')['movie'].agg(list)))
                   .set_index('userid')['movies'].to_dict())
print(recommendations)

{1: ['toy story'], 2: ['john wick'], 3: ['a quite place']}
  • Related