如何根据数据框另一列中的条件在列中查找最小值?

时间:2019-08-28 13:16:24

标签: python pandas

我有一个如下数据框:

Number  Req Response 
0       3    6
1       5    0
2       33   4
3       15   3
4       12   2

我想在“ Req”为15之前确定最小的“ Response”值。

我尝试了以下代码:

min_val=[]
for row in range(len(df)):
#if the next row of 'Req' contains 15, append the current row value of'Response'
  if(df[row+1].loc[df[row+1]['Req'] == 15]): 
         min_val.append(df['Response'].min())
  else:
         min_val.append(0)

我收到“无效类型比较”错误。

我期望以下输出:

Min value of df['Response'] is: 0

2 个答案:

答案 0 :(得分:3)

如果可能的值15不在数据中,请使用常规解决方案:

df = df.reset_index(drop=True)
out = df.loc[df.Req.eq(15)[::-1].cumsum().ne(0), 'Response'].sort_values()
print (out)
1    0
3    3
2    4
0    6
Name: Response, dtype: int64

print (next(iter(out), 'no match'))
0

详细信息

print (df.Req.eq(15))
0    False
1    False
2    False
3     True
4    False
Name: Req, dtype: bool

print (df.Req.eq(15)[::-1])
4    False
3     True
2    False
1    False
0    False
Name: Req, dtype: bool

print (df.Req.eq(15)[::-1].cumsum())
4    0
3    1
2    1
1    1
0    1
Name: Req, dtype: int32

print (df.Req.eq(15)[::-1].cumsum().ne(0))
4    False
3     True
2     True
1     True
0     True
Name: Req, dtype: bool

测试值不匹配:

print (df)
   Number  Req  Response
0       0    3         6
1       1    5         0
2       2   33         4
3       3  150         3
4       4   12         2


df = df.reset_index(drop=True)
out = df.loc[df.Req.eq(15)[::-1].cumsum().ne(0), 'Response'].sort_values()
print (out)
Series([], Name: Response, dtype: int64)

print (next(iter(out), 'no match'))
no match

答案 1 :(得分:2)

一种方法可能是使用idxmax查找第一个索引,其中Req等于15,使用结果索引数据帧并采用最小的Response

df.loc[:df.Req.eq(15).idxmax(), 'Response'].min()
# 0

位置:

df.Req.eq(15)

0    False
1    False
2    False
3     True
4    False
Name: Req, dtype: bool

然后idxmax将返回第一个True出现的索引,在这种情况下为3