How to get value in pivot table based on conditions

sp1r0u :

given a dataframe

import pandas as pd
data = [[1, 10, -1], [1,  7, -2], [1, 3, 14], 
    [2, 11,  1], [2, -2,  2], [1, 13, 4]]
DfObj = pd.DataFrame (data, columns = ['A', 'B', 'C'])

enter image description here

and the pivot table

pivot = DfObj.pivot_table (index=['A', 'B'])

enter image description here

I'd like to get the corresponding C-value to the highest B-value for each A-value, i.e. 4 and 1.

4 is the C-value corresponding to the highest B-value in category A=1.

I assume B-values are unique within each category A.

thank you

yatu :

No need to pivot here. You can use sort_values and groupby.tail:

DfObj.sort_values(['A', 'B']).groupby('A').C.tail(1)

5    4
3    1
Name: C, dtype: int64

Or using last:

DfObj.sort_values(['A', 'B']).groupby('A').C.last()

Another approach could be to use GroupBy.idxmax:

DfObj.loc[DfObj.groupby('A').B.idxmax().values, 'C']

5    4
3    1
Name: C, dtype: int64

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=17975&siteId=1