Reputation: 18638
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 ????
Upvotes: 6
Views: 28403
Reputation: 645
I found this is faster than @Max (last answer) Imagine you have a list of titles which might be empty another method which I found is faster is:
nan_COLUMN_indexes=list(df.loc[pd.isna(df["COLUMN"]), :].index.values)
len(nan_COLUMN_indexes)
By doing this not only do you get the list of all indexes empty in the column but can then :
iterator=iter(nan_COLUMN_indexes)
next(itr,nan_COLUMN_indexes)
This allows you to fill and track CURRENT nan/null value or else pandas would return the First Occurence of the nulls/nan :D
or replace pd.nan()
with pd.isnull()
as per your requirement
Upvotes: 0
Reputation: 1612
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
Upvotes: 2
Reputation: 393863
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
Upvotes: 16