Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 426
J_Heads Avatar asked Sep 06 '16 01:09

J_Heads


People also ask

Can you iterate through rows of a Pandas DataFrame?

Iterating over the rows of a DataFrameYou can do so using either iterrows() or itertuples() built-in methods.

How do you get Groupby index in pandas?

How to perform groupby index in pandas? Pass index name of the DataFrame as a parameter to groupby() function to group rows on an index. DataFrame. groupby() function takes string or list as a param to specify the group columns or index.


1 Answers

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

like image 59
piRSquared Avatar answered Sep 21 '22 19:09

piRSquared