Data mart practie problem code causing error when imputing data

machine_learning
data_science
python

#1

Hey,
this is article link: https://www.analyticsvidhya.com/blog/2016/02/bigmart-sales-solution-top-20

I’m facing an error while trying to perform imputation for Item_Weight as suggested:(I’ve tried in python 2.7 and 3.6)
code:
data.loc[miss_bool,‘Item_Weight’] = data.loc[miss_bool,‘Item_Identifier’].apply(lambda x: item_avg_weight[x])

error:
C:\Users****************\envs\python27\lib\site-packages\pandas\core\indexes\base.pyc 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/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: ‘FDP10’

Any solution would be much appreciated: Thank you.


#2

@kevinkingsman,

The error it is showing for “FDP10”, I looked at the data and found that it is present in the “Item_Identifier” column but we are imputing values of “Item_Weight” column.

After looking at the code, I found a typo, this should be your code(look at the bold part) -

data.loc[miss_bool,‘Item_Weight’] = data.loc[miss_bool,‘Item_Weight’].apply(lambda x: item_avg_weight[x])

Let me know if you face any issues!

@jalFaizy what do you think?

Sanad :slight_smile:


#3

Hi,

I tried with ‘Item_Identifier’ also; it worked.

But only thing i changed is storing pivot table values to pandas series array.

thanks,
Abin