数据框中的KeyError

KeyError in Dataframe

我有一个数据框,当我将它导出到 csv 文件时,它看起来就像我想要的那样。

CompanyName 1   2   3   4   5   6   7   8   9   10  11  12
Company 1   182 270 278 314 180 152 110 127 129 117 127 81
Company 2   163 147 192 142 186 231 214 130 112 117 93  101
Company 3   126 88  99  139 97  97  96  37  79  116 111 95
Company 4   84  89  71  95  80  89  83  88  104 93  78  64

但是,当我尝试从密钥 'CompanyName' 中拉出时,我得到一个 KeyError: 'CompanyName' 我怀疑它在某处被覆盖,但我不确定如何修复它。

如果我打印我的数据框,我得到:

pivot_table.head(2)
Out[62]: 
Month                  1    2    3    4    5    6    7    8    9    10   11  CompanyName                                                                   
Company 1       182  270  278  314  180  152  110  127  129  117  127   
Company 2       163  147  192  142  186  231  214  130  112  117   93   

Month                  12  
CompanyName                
Company 1        81  
Company 2       101  

这很难读懂,无法分辨发生了什么。抛出错误的代码:

pivot_table['CompanyName'] = [str(x) for x in pivot_table['CompanyName']]
Companies = list(pivot_table['CompanyName'])
months = ["1","2","3","4","5","6","7","8","9","10","11","12"]
pivot_table = pivot_table.set_index('CompanyName')

编辑

Bleh 的回答帮助消除了这个 KeyError。我需要通过重置索引来启动代码,因为它无法调用之前已成为索引的 Key。

这是因为您已将索引设置为 CompanyName

您不能以这种方式引用索引。

使用pivot_table = pivot_table.reset_index()重置索引并尝试再次访问它。

这是重现的错误:

 In [45]: df = pd.read_clipboard()

In [46]: df
Out[46]: 
         CompanyName    1    2    3    4    5    6    7    8    9   10   11  \
Company            1  182  270  278  314  180  152  110  127  129  117  127   
Company            2  163  147  192  142  186  231  214  130  112  117   93   
Company            3  126   88   99  139   97   97   96   37   79  116  111   
Company            4   84   89   71   95   80   89   83   88  104   93   78   

          12  
Company   81  
Company  101  
Company   95  
Company   64  

In [47]: df['CompanyName']
Out[47]: 
Company    1
Company    2
Company    3
Company    4
Name: CompanyName, dtype: int64

In [48]: df = df.set_index('CompanyName')

In [49]: df['CompanyName']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-49-d5b597a2bc80> in <module>()
----> 1 df['CompanyName']

/Library/Python/2.7/site-packages/pandas-0.16.1-py2.7-macosx-10.10-intel.egg/pandas/core/frame.pyc in __getitem__(self, key)
   1789             return self._getitem_multilevel(key)
   1790         else:
-> 1791             return self._getitem_column(key)
   1792 
   1793     def _getitem_column(self, key):

/Library/Python/2.7/site-packages/pandas-0.16.1-py2.7-macosx-10.10-intel.egg/pandas/core/frame.pyc in _getitem_column(self, key)
   1796         # get column
   1797         if self.columns.is_unique:
-> 1798             return self._get_item_cache(key)
   1799 
   1800         # duplicate columns & possible reduce dimensionaility

/Library/Python/2.7/site-packages/pandas-0.16.1-py2.7-macosx-10.10-intel.egg/pandas/core/generic.pyc in _get_item_cache(self, item)
   1082         res = cache.get(item)
   1083         if res is None:
-> 1084             values = self._data.get(item)
   1085             res = self._box_item_values(item, values)
   1086             cache[item] = res

/Library/Python/2.7/site-packages/pandas-0.16.1-py2.7-macosx-10.10-intel.egg/pandas/core/internals.pyc in get(self, item, fastpath)
   2849 
   2850             if not isnull(item):
-> 2851                 loc = self.items.get_loc(item)
   2852             else:
   2853                 indexer = np.arange(len(self.items))[isnull(self.items)]

/Library/Python/2.7/site-packages/pandas-0.16.1-py2.7-macosx-10.10-intel.egg/pandas/core/index.pyc in get_loc(self, key, method)
   1576         """
   1577         if method is None:
-> 1578             return self._engine.get_loc(_values_from_object(key))
   1579 
   1580         indexer = self.get_indexer([key], method=method)

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3824)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3704)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12349)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12300)()

KeyError: 'CompanyName'

校正输出:

In [50]: df = df.reset_index()

In [51]: df['CompanyName']
Out[51]: 
0    1
1    2
2    3
3    4
Name: CompanyName, dtype: int64