'Pandas groupby using agg and apply at the same time
I have the following dataframe:
df = pd.DataFrame({'id': [1,1,1,2,3,2], 'year': ['2020', '2014', '2002', '2020', '2016', '2014'], 'e': [True, False, True, True, False, True], 'val': [100,200,300, 200, 300, 200]})
id year e val
0 1 2020 True 100
1 1 2014 False 200
2 1 2002 True 300
3 2 2020 True 200
4 3 2016 False 300
5 2 2014 True 200
And I want the following information:
df.groupby('id').apply(lambda x: x[x['e']]['year'].min())
id
1 2002
2 2014
3 NaN
And
df.groupby('id').val.sum()
id
1 600
2 400
3 300
My question is if it is possible to return both of these in the same group by? Or will I have to do separately and then merge?
Solution 1:[1]
I think using agg
on direct column year
and val
is better than apply
with pd.Series
df_final = df.groupby('id').agg({'year': lambda x: x[df.loc[x.index, 'e']].min(),
'val': 'sum'})
Out[347]:
year val
id
1 2002 600
2 2014 400
3 NaN 300
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andy L. |