在没有“零”值的情况下计算 Pandas 中的最小值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35608893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate minimums in Pandas without `zero`-values?
提问by SpanishBoy
I have a following data and need on first step to find min
values among rows without 0.00
我有以下数据,需要第一步min
在没有行的情况下查找值0.00
HOME_48 HOME_24 HOME_12 HOME_03 HOME_01 HOME_00 HOME
0.00 1.54 2.02 1.84 1.84 1.84 1.84
0.00 1.47 1.76 1.89 2.56 2.56 2.56
0.00 2.02 2.50 2.56 1.89 1.92 1.92
Later I need calculate delta-diff between min
and max
, but if I use below code, the end-results are not acceptable
后来我需要计算min
和之间的增量差异max
,但如果我使用下面的代码,最终结果是不可接受的
df['HOME_MIN'] = df.loc[:, COL_HOME].min(axis=1)
I don't want use following tricks:
我不想使用以下技巧:
df = df.replace(0, np.NaN)
Beacuse, sometimes the extreme values can be equal as 0.01
, 0.02
- these ones are not correct values also.
因为,有时极端值可能等于0.01
, 0.02
- 这些也不是正确的值。
How can I add condition to skip 0.00
| 0.01
values?
如何添加条件以跳过0.00
| 0.01
价值观?
NOTE: correct filter is
注意:正确的过滤器是
df[df[COL_HOME].min(axis=1) > 0.03].loc[:, COL_HOME].min(axis=1)
回答by chrisb
You could use a boolean filter to exclude whatever you don't want, like this.
您可以使用布尔过滤器来排除您不想要的任何内容,就像这样。
In [46]: df[df > .01].min(axis=1)
Out[46]:
0 1.54
1 1.47
2 1.89
dtype: float64