首页 > 解决方案 > 显示新数据集的前五行,在 pandas 中使用 .isnull 显示一列中的所有 NaN 行

问题描述

我有一个天气数据集,我将其剥离为仅在使用 .isnull 显示 NaN 值的列中找到那些行。我想使用 .head 显示这个新数据集的前五行,但系统会显示整个表的前五行(即使是带有值的那些)。如何使用这个新创建的数据集,显示这些行的前五行,将 NaN 显示为值?起初,我使用 london = read_csv('London_2014.csv') 为 excel 表命名为“london”。然后我使用代码 london[london['Max Gust SpeedKm/h'].isnull() 仅显示“Max Gust SpeedKm/h”列的值“NaN”。当我尝试重命名新创建的数据集时,如下所示: londonNew=london[london['Max Gust SpeedKm/h'].isnull()] 然后尝试 londonNew=london[london['Max Gust SpeedKm/h']。获取项目(self f, key)2804 if is_iterator(key)2805 key = list(key)-> 2806 indexer =self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]2807 2808 # take(不接受布尔索引器/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _get_listlike_indexer(self, key, axis, raise_missing) – 1551 1552 self._validate_read_indexer(-> 1553 keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing1554)1555 返回 keyarr, indexer/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in_validate_read_indexer(self, key, indexer, axis, raise_missing) 1638 if missing == len(indexer):1639 axis_name = self.obj._get_axis_name(axis-> 1640 raiseKeyError(f"None of [{key}] are in the [{axis_name}]")1641 1642 #我们(暂时) 允许使用 .loc 丢失一些键,但 inKeyError 除外:"[Float64Index([nan, nan, nan, nan, nan], dtype='float64')] 中没有一个在 [columns]" -</p>

我不确定我做错了什么。

非常感谢您的帮助!印加

标签: pandasheadisnull

解决方案


我认为正在发生的一切是您忘记将新视图分配给变量。尝试这个:

new_london = london[london['Max Gust SpeedKm/h'].isnull()]
new_london.head()

或者,如果您想将数据框更改london为仅使用此新数据:

london = london[london['Max Gust SpeedKm/h'].isnull()]
london.head()

推荐阅读