J_Heads J_Heads - 2 months ago 8
Python Question

Iterate through the rows of a dataframe and reassign minimum values by group

I am working with a dataframe that looks like this.

id time diff
0 0 34 nan
1 0 36 2
2 1 43 7
3 1 55 12
4 1 59 4
5 2 2 -57
6 2 10 8


What is an efficient way find the minimum values for 'time' by id, then set 'diff' to nan at those minimum values. I am looking for a solution that results in:

id time diff
0 0 34 nan
1 0 36 2
2 1 43 nan
3 1 55 12
4 1 59 4
5 2 2 nan
6 2 10 8

Answer

groupby('id') and use idxmin to find the location of minimum values of 'time'. Finally, use loc to assign np.nan

df.loc[df.groupby('id').time.idxmin(), 'diff'] = np.nan
df

enter image description here

Comments