【发布时间】:2015-01-09 16:27:25
【问题描述】:
我有一列 pandas 数据框,它是从带有空白单元格的数据库查询中获得的。空白单元格变为“无”,我想检查每一行是否为无:
In [325]: yes_records_sample['name']
Out[325]:
41055 John J Murphy Professional Building
25260 None
41757 Armand Bayou Nature Center
31397 None
33104 Hubert Humphrey Building
16891 Williams Hall
29618 None
3770 Covenant House
39618 None
1342 Bhathal Student Services Building
20506 None
我对文档的理解是,我可以使用isnull() 命令http://pandas.pydata.org/pandas-docs/dev/missing_data.html#values-considered-missing 检查每一行是否为空
但是,该功能对我不起作用:
In [332]: isnull(yes_records_sample['name'])
我收到以下错误:
NameError Traceback (most recent call last)
<ipython-input-332-55873906e7e6> in <module>()
----> 1 isnull(yes_records_sample['name'])
NameError: name 'isnull' is not defined
我还看到有人刚刚替换了“None”字符串,但这种方法的这些变体都不适合我: Rename "None" value in Pandas
yes_records_sample['name'].replace('None', "--no value--")
yes_records_sample['name'].replace(None, "--no value--")
我最终能够使用 fillna 函数并用空字符串 yes_records_sample.fillna('') 填充每一行作为解决方法,然后我可以检查 yes_records_sample['name']=='' 但我对“无”的工作原理和这是什么意思。有没有办法轻松检查数据框中的单元格是否为“无”?
【问题讨论】: