def top_n_songs(data, n, min_play_counts=100):
recommendations = data[data['play_freq'] > min_play_counts]
recommendations = recommendations.sort_values(by='avg_count', ascending=False)
return recommendations.index[:n]
res = list(top_n_songs(final_play, 10, 100))
list_of_songs = []
for i in res:
list_of_songs.append(df_final[df_final['song_id']== str(i) ]['title'].unique()[0])
list_of_songs
Error message
IndexError Traceback (most recent call last)
<ipython-input-52-7c7c6897126b> in <module>
4 list_of_songs = []
5 for i in res:
----> 6 list_of_songs.append(df_final[df_final['song_id']== str(i) ]['title'].unique()[0])
7 list_of_songs
IndexError: index 0 is out of bounds for axis 0 with size 0
I am a beginner, tried my level best can you give me a hand?
CodePudding user response:
If str(i) == song_id
doesn't match anything in df_final
, the array returned by .unique()
will be empty, causing an IndexError. Make sure to check that first, before attempting to access it with the subscript ([0]
).
CodePudding user response:
I am not really sure about it but if you remove that last [0]
from line 6
, I think it will solve your error.
list_of_songs.append(df_final[df_final['song_id']== str(i) ]['title'].unique())