I have got a weather data set which I stripped to only find those rows in a column showing an NaN value using .isnull. I'd like to show the first five rows of this new data set using .head, but the system shows the first five rows of the whole table (even those with a value in). How can I work with this newly created data set displaying the first five rows of those rows showing NaN as a value? At first I gave the excel table the name "london" using london = read_csv('London_2014.csv'). Then I used the code london[london['Max Gust SpeedKm/h'].isnull() to show only the values "NaN" for the column "Max Gust SpeedKm/h. When I try to rename the newly created dataset as follows: londonNew=london[london['Max Gust SpeedKm/h'].isnull()] and then try londonNew=london[london['Max Gust SpeedKm/h'].head()] the system threw me the following error: KeyError Traceback (most recent call last) in 1 londonNew=london[london['Max Gust SpeedKm/h'].isnull()]----> 2 londonNew[londonNew['Max GustSpeedKm/h'].head()]/usr/local/lib/python3.6/distpackages/pandas/core/frame.py in getitem(sel 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(does not accept boolean indexers/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 return 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 # We (temporarily)allow for some missing keys with .loc, except inKeyError: "None of[Float64Index([nan, nan, nan, nan, nan], dtype='float64')] are in the [columns]" –
I'm not sure what I'm doing wrong.
Many thanks for your help! Inga
I think all that is happening is that you are forgetting to assign the new view to a variable. Try this:
new_london = london[london['Max Gust SpeedKm/h'].isnull()]
new_london.head()
Or if you want to change the london
dataframe to only work with this new data:
london = london[london['Max Gust SpeedKm/h'].isnull()]
london.head()