Skip to content Skip to sidebar Skip to footer

Pandas Dataframe - Find The Row With Minimum Value Based On Two Columns But Greater Than 0

I have a dataframe with 3 columns: x, y, time. There are a few thousand rows. What I want to do is retrieve the row with the minimum time but I would like that the minimum should n

Solution 1:

Try this:

In[69]: df.loc[df.time>0, 'time'].idxmin()
Out[69]: 3

or

In [72]: df.loc[[df.loc[df.time>0, 'time'].idxmin()]]
Out[72]:
     x   y  time3240199.7

Solution 2:

You can filter out 0 values by query and get index of minimal value by idxmin, last select by loc:

s = df.loc[df.query('time != 0')['time'].idxmin()]
print (s)
x       240.0
y        19.0time9.7
Name: 3, dtype: float64

df = df.loc[[df.query('time != 0')['time'].idxmin()]]print (df)
     x   y  time3240199.7

Solution 3:

You don't need groupby at all. Here's an option with mask/where + loc + idxmin;

df.loc[[df.time.mask(df.time.eq(0)).idxmin()]]

Or,

df.loc[[df.time.where(df.time.ne(0)).idxmin()]]

     x   y  time3240199.7

Post a Comment for "Pandas Dataframe - Find The Row With Minimum Value Based On Two Columns But Greater Than 0"