Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from a dataframe column check if a string is nan

Tags:

python

pandas

From a dataframe when I print data['words'].values 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 ????
like image 779
richie Avatar asked Aug 12 '14 07:08

richie


People also ask

Does Isnull check for NaN?

Detect missing values for an array-like object. This function takes a scalar or array-like object and indicates whether values are missing ( NaN in numeric arrays, None or NaN in object arrays, NaT in datetimelike).

Is NaN in DataFrame?

The official documentation for pandas defines what most developers would know as null values as missing or missing data in pandas. Within pandas, a missing value is denoted by NaN .


2 Answers

Use the pandas method isnull to test:

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
like image 55
EdChum Avatar answered Oct 30 '22 19:10

EdChum


the question was about the if:

for i in range(150,160):
  if (pd.isnull(df['Text'][i])):
    print('is null')

or count all NaN:

cnt=0
for i in range(len(dframe)):
  if (pd.isnull(dframe['Description'][i])):
    cnt+=1
like image 38
Max Kleiner Avatar answered Oct 30 '22 21:10

Max Kleiner