如何检查 <NA> 类型变量是否为 <NA?或者不是来自 pandas 数据框? np.nan() 不工作

How to check if <NA> type variable is <NA? or not from a pandas dataframe? np.nan() not working

我有一个数据框,其中有一列的值类似于:

     YEAR_TORONTO
0    <NA>
1    2016
2    <NA>
3    1999

出于这段代码之外的其他原因,我需要通过 for 循环单独检查此数据帧的每个元素,因此我正在寻找符合我的实现的解决方案。

基本上我目前用来检查是否存在的代码是:

if np.isnan(df.get("YEAR_TORONTO")[row]):

这导致我收到 <NA> 值的以下错误:

boolean value of NA is ambiguous

知道我可以做些什么来修复这个错误吗?帮助非常感谢

正如 sammywemmy 所说,pd.isna() 应该可以。

>>> d = pd.Series([1,2,pd.NA,3])
>>> d
0       1
1       2
2    <NA>
3       3
dtype: object
>>> d.isna()
0    False
1    False
2     True
3    False
dtype: bool