Pandas Dataframe-根据两列找到最小值小于0的行 [英] Pandas Dataframe - find the row with minimum value based on two columns but greater than 0

查看:354
本文介绍了Pandas Dataframe-根据两列找到最小值小于0的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3列的数据框:x,y,时间.有几千行.

I have a dataframe with 3 columns: x, y, time. There are a few thousand rows.

我想做的是用最少的时间来检索行,但我希望最小值不应该为0.

What I want to do is retrieve the row with the minimum time but I would like that the minimum should not be 0.

例如

x     y    time
240   1    28.5
240   2    19.3
240   240     0
240   19    9.7

到目前为止,我已经尝试过以下操作:

So far what I've tried were the following:

df.loc[df['time'] > 0].min()
# this gives me a series and I want a row
# x    225.000000
# y      0.000000
# time   1.066606

df['time'].drop_duplicates().nsmallest(1)
# 225    0.0

我也尝试过groupby

I have also tried something with groupby as well

df.loc[df.groupby('id_x', sort=False)['time'].idxmin()]

我知道在将其替换为子集时会遇到问题,因为我通常会得到一个系列.

I know had problems subsetting this one as I usually got a series.

推荐答案

您可以通过0值.DataFrame.query.html"rel =" nofollow noreferrer> query 并通过

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

这篇关于Pandas Dataframe-根据两列找到最小值小于0的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆