Iterating over rows of a data frame and reassigning minimum values ​​to groups

I am working with a file frame 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 to find the minimum values ​​for "time" by id, and then set the "diff" to nan at these minimum values. I am looking for a solution that leads to:

   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
+4
source share
2 answers

groupby('id')and use idxminto find the location of the minimum values 'time'. Finally, use locto assignnp.nan

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

enter image description here

+6
source

id , , , True, False NaN :

import numpy as np
import pandas as pd
df.loc[df.groupby('id')['time'].apply(lambda g: g == min(g)), "diff"] = np.nan

df
#  id   time    diff
#0  0     34    NaN
#1  0     36    2.0
#2  1     43    NaN
#3  1     55    12.0
#4  1     59    4.0
#5  2      2    NaN
#6  2     10    8.0
+4

Source: https://habr.com/ru/post/1653732/


All Articles