richie richie - 1 year ago 167
Python Question

from a dataframe column check if a string is nan

From a dataframe when I print

I get,

['from' 'fairest' 'creatures' 'we' 'desire' 'increase' nan 'that' 'thereby']

When I loop through like this how do I identify if the value is nan?

for w in data['words'].values:
check if w is nan ????

Answer Source

Use the pandas method isnull to test:

In [45]:

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

0    False
1    False
2    False
3    False
4    False
5    False
6     True
7    False
8    False
Name: words, dtype: bool
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download