@AnnaList wrote:
#Determine average visibility of a product
visibility_avg = data.pivot_table(values=‘Item_Visibility’, index=‘Item_Identifier’)#Impute 0 values with mean visibility of that product:
miss_bool = (data[‘Item_Visibility’] == 0)print(‘Number of 0 values initially: %d’%sum(miss_bool))
data.loc[miss_bool,‘Item_Visibility’] = data.loc[miss_bool,‘Item_Identifier’].apply(lambda x: visibility_avg[x])
print(‘Number of 0 values after modification: %d’%sum(data[‘Item_Visibility’] == 0))I was solving the Big Mart Sales problem but i ended up with the below error.Please help.I am referring the solution provided on the website!
Number of 0 values initially: 879KeyError Traceback (most recent call last)
C:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2524 try:
-> 2525 return self._engine.get_loc(key)
2526 except KeyError:pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: ‘FDX07’
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
in ()
6
7 print(‘Number of 0 values initially: %d’%sum(miss_bool))
----> 8 data.loc[miss_bool,‘Item_Visibility’] = data.loc[miss_bool,‘Item_Identifier’].apply(lambda x: visibility_avg[x])
9 print(‘Number of 0 values after modification: %d’%sum(data[‘Item_Visibility’] == 0))C:\Anaconda\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
2549 else:
2550 values = self.asobject
-> 2551 mapped = lib.map_infer(values, f, convert=convert_dtype)
2552
2553 if len(mapped) and isinstance(mapped[0], Series):pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()
in (x)
6
7 print(‘Number of 0 values initially: %d’%sum(miss_bool))
----> 8 data.loc[miss_bool,‘Item_Visibility’] = data.loc[miss_bool,‘Item_Identifier’].apply(lambda x: visibility_avg[x])
9 print(‘Number of 0 values after modification: %d’%sum(data[‘Item_Visibility’] == 0))C:\Anaconda\lib\site-packages\pandas\core\frame.py in getitem(self, key)
2137 return self._getitem_multilevel(key)
2138 else:
-> 2139 return self._getitem_column(key)
2140
2141 def _getitem_column(self, key):C:\Anaconda\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
2144 # get column
2145 if self.columns.is_unique:
-> 2146 return self._get_item_cache(key)
2147
2148 # duplicate columns & possible reduce dimensionalityC:\Anaconda\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
1840 res = cache.get(item)
1841 if res is None:
-> 1842 values = self._data.get(item)
1843 res = self._box_item_values(item, values)
1844 cache[item] = resC:\Anaconda\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
3841
3842 if not isna(item):
-> 3843 loc = self.items.get_loc(item)
3844 else:
3845 indexer = np.arange(len(self.items))[isna(self.items)]C:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2525 return self._engine.get_loc(key)
2526 except KeyError:
-> 2527 return self._engine.get_loc(self._maybe_cast_indexer(key))
2528
2529 indexer = self.get_indexer([key], method=method, tolerance=tolerance)pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: ‘FDX07’
Posts: 1
Participants: 1