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

#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



Solution 1:[1]

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

Solution 2:[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').

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 TMmn