ValueError: 'movieId' is both an index level and a column label, which is ambiguous?
Asked Answered
E

2

13
#join movie details to movie ratings
movie_score = pd.merge(movie_score,movies_with_genres,on='movieId')
#join movie links to movie ratings
#movie_score = pd.merge(movie_score,links,on='movieId')
movie_score.head()

There is such an error in line 2 in the code , how can I fix it?

enter image description here

Extrasystole answered 25/5, 2021 at 11:20 Comment(0)
S
13

I think you had some indexes in both dataframes mixed up. To resolve that, I suggest to reset each dataframe's indexes as following:

movie_score.reset_index(drop = True, inplace = True)
movies_with_genres.reset_index(drop = True, inplace = True)

movie_score = pd.merge(movie_score,movies_with_genres,on='movieId')
Seaton answered 22/9, 2021 at 18:18 Comment(0)
S
2

If one of the columns is set as an index and on the other table it is not merge is not allowed. A quick fix would be to set the column as the index of the dataframe using df.set_index('movieId').

Staton answered 25/5, 2021 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.