Pandas Dataframe - find the row with minimum value based on two columns but greater than 0

后端 未结 3 1742
南方客
南方客 2021-01-22 16:31

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 minim

3条回答
  •  迷失自我
    2021-01-22 16:41

    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.0
    time      9.7
    Name: 3, dtype: float64
    
    df = df.loc[[df.query('time != 0')['time'].idxmin()]]
    print (df)
         x   y  time
    3  240  19   9.7
    

提交回复
热议问题