python - How to use group by pandas to fetch a column corresponding to a max column? -
my data set looks this
playerid,position,points 1,pos1,10 1,pos2,15 1,pos3,4
i want return position players scored points. can group_by find points particular player, how position?
data.groupby(['playerid']).agg(np.max)['points']
i want return both playerid , position
1,pos2
one way (among many) that:
in [133]: df out[133]: playerid position points 0 1 pos1 10 1 1 pos2 15 2 1 pos3 4 3 2 pos1 2 4 2 pos2 10 5 2 pos3 18 in [134]: df.loc[df.groupby('playerid')['points'].idxmax(), ['playerid','position']] out[134]: playerid position 1 1 pos2 5 2 pos3
Comments
Post a Comment