Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the row corresponding to the max in pandas GroupBy

Simple DataFrame:

df = pd.DataFrame({'A': [1,1,2,2], 'B': [0,1,2,3], 'C': ['a','b','c','d']})
df
   A  B  C
0  1  0  a
1  1  1  b
2  2  2  c
3  2  3  d

I wish for every value (groupby) of column A, to get the value of column C, for which column B is maximum. For example for group 1 of column A, the maximum of column B is 1, so I want the value "b" of column C:

   A  C
0  1  b
1  2  d

No need to assume column B is sorted, performance is of top priority, then elegance.

like image 702
Giora Simchoni Avatar asked Jan 23 '19 19:01

Giora Simchoni


People also ask

How do you get max rows in pandas?

Find Maximum Element in Pandas DataFrame's Row The default value for the axis argument is 0. If the axis equals to 0, the max() method will find the max element of each column. On the other hand, if the axis equals to 1, the max() will find the max element of each row.

How do you get groupby rows in pandas?

You can group DataFrame rows into a list by using pandas. DataFrame. groupby() function on the column of interest, select the column you want as a list from group and then use Series. apply(list) to get the list for every group.


1 Answers

Check with sort_values +drop_duplicates

df.sort_values('B').drop_duplicates(['A'],keep='last')
Out[127]: 
   A  B  C
1  1  1  b
3  2  3  d
like image 189
BENY Avatar answered Oct 27 '22 00:10

BENY