1

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.

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

ValueError: expr must be a string to be evaluated, <class 'list'> given
I have a big data frame in general
If use df1 = df[(df['gender'] == 'ma') & (df['age'] == 'young')] instead df1 = df.query("gender== 'ma' & age == 'young'") same problem?
Thanks jez, it worked. but I have to do it for 5 lakhs rows and 12 columns ... :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.