Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find the max of column group by another column pandas

I have a dataframe with 2 columns:

count percent  grpno.
0          14.78       1
1           0.00       2
2           8.80       3
3           9.60       4
4          55.90       4
5           0.00       2
6           0.00       6
7           0.00       5
8           6.90       1
9          59.00       4

I need to get the max of column 'count percent ' and group by column 'grpno.'. Though I tried doing the same by

geostat.groupby(['grpno.'], sort=False)['count percent'].max()

I get the output to be

grpno.
1    14.78
2     0.00
3     8.80
4    59.00
6     0.00
5     0.00
Name: count percent, dtype: float64

But I need output to be a dataframe that has the column name modified as 'MaxOfcount percent' and 'grpno.' Can anyone help on this? Thanks

like image 379
User1090 Avatar asked Jan 20 '16 18:01

User1090


1 Answers

res = df.groupby('grpno.')['count percent'].max().reset_index()
res.columns = ['grpno.', 'MaxOfcount percent']

   grpno.  MaxOfcount percent
0       1               14.78
1       2                0.00
2       3                8.80
3       4               59.00
4       5                0.00
5       6                0.00

You could also do it in one line:

res = df.groupby('grpno.', as_index=False)['count percent'].max().rename(columns={'count percent': 'MaxOfcount percent'})
like image 188
Stefan Avatar answered Nov 14 '22 22:11

Stefan