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.

User 19826
  • 509
  • 2
  • 5
  • 13

1 Answers1

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
anky
  • 74,114
  • 11
  • 41
  • 70
  • 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? – User 19826 Mar 07 '20 at 06:04
  • 1
    @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 – anky Mar 07 '20 at 06:06