octo
octo

Reputation: 23

Panda DF : Find minimum in multiple columns with condition

In a pandas dataframe, I am trying to find the minimum of different columns that respects a condition in an other column (kind of an SQL WHERE)

MIN = (dataframe[['col_1','col_2','col_3', 'col_4']].min().min())

But i want MIN to be calculated only where 'col_5' == 'YES'

I have tried to find in the forum but i am not managing to merge the 'multiple column min' and the 'conditional min'..

Thank you !

Upvotes: 1

Views: 1164

Answers (1)

Jimmar
Jimmar

Reputation: 4459

so you want something like

MIN = dataframe[dataframe['col_5'] == 'YES']['col_1','col_2','col_3', 'col_4'].min().min()

or a more readable version

dataframe_yes = dataframe[dataframe['col_5'] == 'YES']
MIN = dataframe_yes[['col_1','col_2','col_3', 'col_4']].min().min()

Upvotes: 1

Related Questions