Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a Pandas dataframe by a condition and a minimum value in a column

Tags:

python

pandas

I have a dataframe similar to

  test_a test_b  metric_e
0     OK    NOK        12
1     OK     OK         7
2     OK    NOK         2
3     OK     OK        55

and I want to filter by one condition, meaning that test_a == OK and capture the minimum value on metric_e. I can accomplish that with two lines, copying a dataframe:

df_t = df[df.test_a == 'OK'].reset_index(drop=True)
df_t.iloc[df_t.metric_e.idxmin()].to_frame()

test_a | test_b | metric_e
OK     |  NOK   | 2

Is there a way to do it without having to use an intermediate dataframe?

like image 208
Ivan Avatar asked Dec 11 '22 03:12

Ivan


2 Answers

Using nsmallest:

df[df['test_a']=='OK'].nsmallest(1, 'metric_e')

Output:

  test_a test_b  metric_e
2     OK    NOK         2
like image 77
perl Avatar answered Dec 21 '22 18:12

perl


In my opinion your solution is nice, also is possible join both rows of code together with double [] for return one row DataFrame:

df = df.loc[[df.loc[df.test_a == 'OK', 'metric_e'].idxmin()]]
print (df)
  test_a test_b  metric_e
2     OK    NOK         2
like image 22
jezrael Avatar answered Dec 21 '22 16:12

jezrael