如何使用pandas DataFrame中的pivot_table分组替换NaN值?

时间:2021-05-09 15:38:38

标签: python pandas

我正在处理一个机器学习练习问题,从 https://datahack.analyticsvidhya.com/contest/practice-problem-big-mart-sales-iii/#ProblemStatement 我想替换列“Item_Weight”中的空值,为此我使用了一个pivot_table给出的平均值,在那里我计算了平均值'Item_Weight' 并按数据集的 'Item_Identifier' 列对平均值进行分组。

item_weight_mean = ds.pivot_table(values='Item_Weight', columns='Item_Identifier')
loc2 = ds['Item_Weight'].isnull()
ds.loc[loc2, 'Item_Weight'] = ds.loc[loc2, 'Item_Identifier'].apply(lambda x: item_weight_mean[x])

我收到相同代码的错误。

(key)
-> 2902             indexer = self.columns.get_loc(key)
   2903             if is_integer(indexer):
   2904                 indexer = [indexer]

D:\Important Applications\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2895                 return self._engine.get_loc(casted_key)
   2896             except KeyError as err:
-> 2897                 raise KeyError(key) from err
   2898 
   2899         if tolerance is not None:

KeyError: 'FDN52'

我该如何消除这个错误?我在这里犯了什么错误?我卡住了。

0 个答案:

没有答案