I am just getting to know pandas and I can't get over a conceptual problem. My dataframe is as follows:
df=pd.DataFrame({'ANIMAL':[1,1,1,1,1,2,2,2],
'AGE_D' : [3,6,47,377,698,1,9,241],
'AGE_Y' : [1,1,1,2,2,1,1,1]})
I would like to do a nested group within animal and age_y and then select the min on the subgroup. Desired output would be then:
ANIMAL AGE_Y AGE_D
1 1 3
1 2 377
2 1 1
I can do this without nesting within animal, e.g. if my df2 = subset for ANIMAL=1 then
df2.loc[df2.groupby('AGE_Y')['AGE_D'].idxmin()]
But all the things I tried with nesting the animal in the group by were unsuccesful. I am guessing that my order of the operations is wrong... How should I go about this?