[英]lambda function to find if values are boolean
I have the following DF:我有以下DF:
is_bool
True
False
NaN
foo
3
I am looking to do validation on this column using pandas_schema.我希望使用 pandas_schema 对此列进行验证。 Here is the function I've written which I think is incorrect:
这是我写的function,我认为是不正确的:
def check_bool(byte):
try:
bool(byte)
except ValueError:
return False
return True
bool_validation = [CustomElementValidation(
lambda b: check_bool(b), 'Value is not is boolean format')]
The output is supposed to tell when a value is not True Or False: output 应该告诉一个值何时不是真或假:
row 2 is 'Value is not is boolean format'
row 3 is 'Value is not is boolean format'
row 4 is 'Value is not is boolean format'
You can use pandas.Series.apply
:您可以使用
pandas.Series.apply
:
>>> df.is_bool.apply(isinstance, args = (bool,))
0 True
1 True
2 False
3 False
4 False
Name: is_bool, dtype: bool
I think instead try-except
is possible use isinstance
with bool
:我认为
try-except
可以将isinstance
与bool
一起使用:
df = pd.DataFrame({'is_bool':['', True, False, np.nan, 'foo', 3]})
def check_bool(x):
return isinstance(x, bool)
print (df['is_bool'].apply(check_bool))
0 False
1 True
2 True
3 False
4 False
5 False
Name: is_bool, dtype: bool
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.