Home > Mobile >  How to find the minimum value greater than 0 in a pandas df?
How to find the minimum value greater than 0 in a pandas df?

Time:06-29

Suppose I got the following df_percentage_change:

Index End Date                Change
0     2022-06-18 19:59:59.999 0
1     2022-06-18 20:29:59.999 -0.020028
2     2022-06-18 20:59:59.999 1.53509
3     2022-06-18 21:29:59.999 2.30751

How can I get the minimum value greater than 0 in the column Change (which is 1.53509) using a single line of code?

I tried the following:

df_percentage_change["Change"].apply(lambda x: min(df_percentage_change["Change"]) if df_percentage_change["Change"] > 0)

But got:

SyntaxError: invalid syntax

CodePudding user response:

Already figured it out:

In [1]: min(filter(lambda x: x > 0, df_percentage_change["Change"]))

Out[2]: 1.53509

  • Related