经常遇到空值None,或是pandas有单元格为空,或是’ '等无价值的数值情况,这里总结一个函数,来识别他们:
import pandas as pd
def check_something(number):
if pd.isna(number) or str(number).strip() == '':
return False
return True
print(check_something(None))
print(check_something(pd.NA))
print(check_something(' '))
print(check_something(''))
print(check_something('a'))
print(check_something(123))
当然也可以简写为:
def check_something(number):
return False if pd.isna(number) or str(number).strip() == '' else True
或是匿名函数:
x = ' '
result = (lambda number: False if pd.isna(number) or str(number).strip() == '' else True)(x)
print(result)