pandas 比较熊猫列中的浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33626443/
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
Comparing floats in a pandas column
提问by darkpool
I have the following dataframe:
我有以下数据框:
actual_credit min_required_credit
0 0.3 0.4
1 0.5 0.2
2 0.4 0.4
3 0.2 0.3
I need to add a column indicating where actual_credit >= min_required_credit. The result would be:
我需要添加一列指示实际信用 >= min_required_credit 的位置。结果将是:
actual_credit min_required_credit result
0 0.3 0.4 False
1 0.5 0.2 True
2 0.4 0.4 True
3 0.1 0.3 False
I am doing the following:
我正在做以下事情:
df['result'] = abs(df['actual_credit']) >= abs(df['min_required_credit'])
However the 3rd row (0.4 and 0.4) is constantly resulting in False. After researching this issue at various places including: What is the best way to compare floats for almost-equality in Python?I still can't get this to work. Whenever the two columns have an identical value, the result is False which is not correct.
然而,第三行(0.4 和 0.4)不断导致 False。在多个地方研究了这个问题之后,包括:在 Python 中比较浮点数以实现几乎相等的最佳方法是什么?我仍然无法让这个工作。每当两列具有相同的值时,结果为 False,这是不正确的。
I am using python 3.3
我正在使用 python 3.3
回答by EdChum
Due to imprecise float comparison you can or
your comparison with np.isclose
, isclose
takes a relative and absolute tolerance param so the following should work:
由于不精确的浮点比较,您可以or
与np.isclose
,isclose
进行比较,采用相对和绝对容差参数,因此以下内容应该有效:
df['result'] = df['actual_credit'].ge(df['min_required_credit']) | np.isclose(df['actual_credit'], df['min_required_credit'])
回答by Tomasz Bartkowiak
In general numpy
Comparisonfunctions work well with pd.Series
and allow for element-wise comparisons:
isclose
, allclose
, greater
, greater_equal
, less
, less_equal
etc.
一般来说numpy
比较功能与正常工作pd.Series
,并允许逐元素的比较:
isclose
,allclose
,greater
,greater_equal
,less
,less_equal
等。
In your case greater_equal
would do:
在你的情况下greater_equal
会这样做:
df['result'] = np.greater_equal(df['actual_credit'], df['min_required_credit'])
or alternatively, as proposed, using pandas.ge
(alternatively le
, gt
etc.):
或替代地,所建议,使用pandas.ge
(可替换地le
,gt
等):
df['result'] = df['actual_credit'].ge(df['min_required_credit'])
The risk with or
ing with ge
(as mentioned above) is that e.g. comparing 3.999999999999
and 4.0
might return True
which might not necessarily be what you want.
使用or
ing with ge
(如上所述)的风险在于,例如比较3.999999999999
并且4.0
可能返回True
这可能不一定是您想要的。
回答by NPE
Use pandas.DataFrame.abs()
instead of the built-in abs()
:
使用pandas.DataFrame.abs()
而不是内置的abs()
:
df['result'] = df['actual_credit'].abs() >= df['min_required_credit'].abs()