Is it possible to get the min and max values from two series in a groupby?
For example in the following situation, when grouping by c
, how can I get the min and max values for a
and b
at the same time?
df = pd.DataFrame({'a': [10,20,3,40,55], 'b': [5,14,8,50,60], 'c': ['x','x','y','y','y']})
g = df.groupby(df.c)
for key, item in g:
print (g.get_group(key), "\n")
a b c
0 10 5 x
1 20 14 x
a b c
2 3 8 y
3 40 50 y
4 55 60 y
I have resolved this by taking the min and max of each grouped series then by finding the min and max of the _min
/_max
series:
df['a_min'] = g['a'].transform('min')
df['a_max'] = g['a'].transform('max')
df['b_min'] = g['b'].transform('min')
df['b_max'] = g['b'].transform('max')
df['min'] = df[['a_min', 'a_max', 'b_min', 'b_max']].min(axis=1)
df['max'] = df[['a_min', 'a_max', 'b_min', 'b_max']].max(axis=1)
a b c a_min a_max b_min b_max min max
0 10 5 x 10 20 5 14 5 20
1 20 14 x 10 20 5 14 5 20
2 3 8 y 3 55 8 60 3 60
3 40 50 y 3 55 8 60 3 60
4 55 60 y 3 55 8 60 3 60
This produces the output that I want but with a lot of extra series. I am wondering if there is a better way to do this?