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.
答
您可以通过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