I am trying to figure out, how to count unique values in some column depending on another value. My DataFrame looks like this:
id_user id_track
1 1
1 2
1 4
3 1
3 1
3 4
1 1
2 5
Basically I have a table with id's of users and songs they listened to. I want to count for each user, how many unique songs did he listen and to sort it by this value. The output should be like this:
id_user uniqueTracks
1 3
3 2
2 1
I tried to do this in this way (hearings is my DataFrame):
uniqueTracks=[] #list of numbers of unique tracks
for i in range(len(hearings['id_user'].unique())):
uniqueTracks.append(len(hearings[hearings['id_user']==i['titles'].unique()))
but it works terribly slow for table of 27 million rows and 70k unique users. Does anyone have a clue how to do this in Pandas? Thank you in advance:)