您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

有效检查Python / numpy / pandas中是否有任意对象是NaN?

有效检查Python / numpy / pandas中是否有任意对象是NaN?

pandas.isnull()(也是pd.isna(),在较新的版本中)检查数字数组和字符串/对象数组中的缺失值。从文档中,它检查:

数字数组中的NaN,对象数组中的None / NaN

快速示例:

import pandas as pd
import numpy as np
s = pd.Series(['apple', np.nan, 'banana'])
pd.isnull(s)
Out[9]: 
0    False
1     True
2    False
dtype: bool

numpy.nan用来表示缺失值的想法是pandas引入的,这就是为什么pandas有工具来处理它的原因。

日期时间也是如此(如果使用pd.NaT,则无需指定dtype)

In [24]: s = Series([Timestamp('20130101'),np.nan,Timestamp('20130102 9:30')],dtype='M8[ns]')

In [25]: s
Out[25]: 
0   2013-01-01 00:00:00
1                   NaT
2   2013-01-02 09:30:00
dtype: datetime64[ns]``

In [26]: pd.isnull(s)
Out[26]: 
0    False
1     True
2    False
dtype: bool
python 2022/1/1 18:44:19 有305人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶