How To Use Columns Values To Groupby
I need to get the top1 and top2 rating watched by 'ma' and 'young'. here I only need to specifically define my value but not column usinga group by. data: gender age rating ma yo
Solution 1:
First filter and then get tops, but general is possible second top should not exist:
df1 = df.query("gender== 'ma' & age == 'young'")
#alternative is boolean indexing
#df1 = df[(df['gender'] == 'ma') & (df['age'] == 'young')]
tops = df1.groupby(['gender','age'])['rating'].value_counts()
print (tops)
gender age rating
ma young PG 2
R 1
print (df.iloc[[0]])
gender age rating
0 ma young PG
print (df.iloc[[1]])
gender age rating
1 fe young PG
Post a Comment for "How To Use Columns Values To Groupby"