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
groupby('id')and use idxminto find the location of the minimum values 'time'. Finally, use locto assignnp.nan
groupby('id')
idxmin
'time'
loc
np.nan
df.loc[df.groupby('id').time.idxmin(), 'diff'] = np.nan df
id , , , True, False NaN :
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
Source: https://habr.com/ru/post/1653732/More articles:Why am I forced to specify a type here? - haskellImage Hover does not show ...? - cssHandling firebase auth with a relay - javascriptWebpack Hot Reload with Angular2? - javascriptconverting ajax request to angular $ http - angularjsCorrected exception with CompletableFuture - javaВызов события MongoDB после ввода данных в коллекцию mongoDB - javaIE11 incorrectly calculates parent flex container height - htmlPHP - получение последних операторов PK ID для использования во второй функции. Вставка выражения - functionThrow checked exceptions using CompletableFuture - javaAll Articles