gpt4 book ai didi

python - 从数据框列检查字符串是否为 nan

转载 作者:行者123 更新时间:2023-11-28 20:22:01 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

使用 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

关于python - 从数据框列检查字符串是否为 nan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25258460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com