i got error none of the index are in the column in python

Viewed 149

I have this problem with juputer notebook when i wright that line in python

df["AQI"] = df["AQI"].fillna(round(df[["PM2.5_SubIndex", "PM10_SubIndex", "SO2_SubIndex", "NOx_SubIndex","NH3_SubIndex", "CO_SubIndex", "O3_SubIndex"]].max(axis = 1)))

the whole project here: here

the error is :

**KeyError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_6320/515642622.py in ----> 1 default["AQI"] = default["AQI"].fillna(round(default[["PM2.5_SubIndex", "PM10_SubIndex", "SO2_SubIndex", "NOx_SubIndex","NH3_SubIndex", "CO_SubIndex", "O3_SubIndex"]].max(axis = 1))) 2 ~\miniconda3\lib\site-packages\pandas\core\frame.py in getitem(self, key) 3462 if is_iterator(key): 3463 key = list(key) -> 3464 indexer = self.loc._get_listlike_indexer(key, axis=1)1 3465 3466 # take() does not accept boolean indexers ~\miniconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis) 1312 keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr) 1313 -> 1314 self._validate_read_indexer(keyarr, indexer, axis) 1315 1316 if needs_i8_conversion(ax.dtype) or isinstance( ~\miniconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis) 1372 if use_interval_msg: 1373 key = list(key) -> 1374 raise KeyError(f"None of [{key}] are in the [{axis_name}]") 1375 1376 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique())

and key error

KeyError: "None of [Index(['PM2.5_SubIndex', 'PM10_SubIndex', 'SO2_SubIndex', 'NOx_SubIndex',\n 'NH3_SubIndex', 'CO_SubIndex', 'O3_SubIndex'],\n dtype='object')] are in the [columns]"

can anyone explain to me what the error and how to solve it

1 Answers

I think your axis=1 should be outside of the max() function call. You could rewrite is as:

df["AQI"] = df["AQI"].fillna(round(df[["PM2.5_SubIndex", "PM10_SubIndex", "SO2_SubIndex", "NOx_SubIndex","NH3_SubIndex", "CO_SubIndex", "O3_SubIndex"]].max()), axis=1)
Related