Reputation: 1460
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 young PG
fe young PG
ma adult PG
fe adult PG
ma young PG
fe young PG
ma adult R
fe adult R
ma young R
fe young R
code :
top1 = df.groupby(['ma','young']])['rating'].apply(lambda x: x.value_counts().index[0])
top2 = df.groupby(['ma','young']])['rating'].apply(lambda x: x.value_counts().index[1])
Please let me know how do i do it.
Upvotes: 1
Views: 61
Reputation: 862651
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
Upvotes: 2