pandas 从数据框列检查字符串是否为 nan
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25258460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:21:50 来源:igfitidea点击:
from a dataframe column check if a string is nan
提问by richie
From a dataframe when I print data['words'].valuesI get,
从我打印时的数据框中data['words'].values得到,
['from' 'fairest' 'creatures' 'we' 'desire' 'increase' nan 'that' 'thereby']
When I loop through like this how do I identify if the value is nan?
当我这样循环时,如何确定值是否为 nan?
for w in data['words'].values:
check if w is nan ????
回答by EdChum
Use the pandas method isnullto test:
使用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
回答by Max Kleiner
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:
或计算所有 NaN:
cnt=0
for i in range(len(dframe)):
if (pd.isnull(dframe['Description'][i])):
cnt+=1

