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

Viewed 6709
#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

2 Answers

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')

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').

Related