I have the following dataframe:
actual_credit min_required_credit
0 0.3 0.4
1 0.5 0.2
2 0.4 0
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:
df['result'] = df['actual_credit'].ge(df['min_required_credit']) | np.isclose(df['actual_credit'], df['min_required_credit'])
Use pandas.DataFrame.abs()
instead of the built-in abs()
:
df['result'] = df['actual_credit'].abs() >= df['min_required_credit'].abs()
In general numpy
Comparison functions work well with pd.Series
and allow for element-wise comparisons:
isclose
, allclose
, greater
, greater_equal
, less
, less_equal
etc.
In your case greater_equal would do:
df['result'] = np.greater_equal(df['actual_credit'], df['min_required_credit'])
or alternatively, as proposed, using pandas.ge(alternatively le
, gt
etc.):
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.