1

Similar to a previous question, I need to use the apply function to some data after using groupBy.

Suppose we have the following dataset with column heads A,B,C:

 a 0 2
 b 0 3
 c 0 4
 a 1 1
 b 1 2
 c 1 5
 a 0 3
 b 1 2
 c 0 1

and the following group by is applied:

   mydf=df.groupby(['A','B'])['C'].max()

I need to filter the group by result by a filter to show only those where C is 3, but get an error.

       print(mydf.apply(lambda g: g[g['C'] == 3]))

Thank you in advance.

1 Answer 1

1

You can use .loc[] with a lambda here:

df.groupby(['A','B'])['C'].max().loc[lambda x: x==3]

A  B
a  0    3
b  0    3
Name: C, dtype: int64
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, I accepted the answer. If you don't mind, could you also tell me how to apply the same filter on either A or B, after performing the group function?
@Fabiana you can reset the index and use loc example: df.groupby(['A','B'])['C'].max().reset_index().loc[lambda x:x['A']=='a'] which will filter column A equal to a for the grouped value

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.