从数据帧列检查字符串是否为nan

ric*_*hie 2 python pandas

从我打印的数据框中data['words'].values得到,

['from' 'fairest' 'creatures' 'we' 'desire' 'increase' nan 'that' 'thereby']
Run Code Online (Sandbox Code Playgroud)

当我像这样循环时,如何确定值是否为nan?

for w in data['words'].values:
    check if w is nan ????
Run Code Online (Sandbox Code Playgroud)

EdC*_*ica 8

使用pandas方法isnull测试:

In [45]:

df = pd.DataFrame({'words':['from', 'fairest', 'creatures' ,'we' ,'desire', 'increase' ,nan ,'that' ,'thereby']})
df
Out[45]:
       words
0       from
1    fairest
2  creatures
3         we
4     desire
5   increase
6        NaN
7       that
8    thereby
In [46]:

pd.isnull(df['words'])
Out[46]:
0    False
1    False
2    False
3    False
4    False
5    False
6     True
7    False
8    False
Name: words, dtype: bool
Run Code Online (Sandbox Code Playgroud)