Home > Software engineering >  Dataframe multiple condition
Dataframe multiple condition

Time:03-28

I have the following table

    Wh    Cost  Capacity
 0  NaN   0.0      NaN
 1  5.0  2350.0    500.0
 2  4.0  2080.0    0.0
 3  7.0  2890.0    500.0

I want to select the WH with the least cost and where the capacity not equal 0 the output will be 5.0

Im not sure how to get it

CodePudding user response:

You can filter the dataframe based on capacity not equal to zero and then find the min of the resulted dataframe and check the min cost and compare it with Cost column. Refer the below solution:

filteredDf = df[df['Cost']==df[(df['Capacity']!=0)&(df['Cost']!=0)]['Cost'].min()]

Output:

    Wh    Cost  Capacity
0  5.0  2350.0     500.0

CodePudding user response:

df[df['Cost'] == df[(df['Capacity'] != 0) & (df['Cost'] != 0)]['Cost'].min()] it gives me the result

CodePudding user response:

In addition to @Vaibhav answer, you can add Wh column at the end to get precise answer.

filteredDf = df[df['Cost']==df[(df['Capacity']!=0)&(df['Cost']!=0)]['Cost'].min()].Wh
  • Related