Python 数据帧中的关键错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31167896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:35:27  来源:igfitidea点击:

KeyError in Dataframe

pythonpandasdataframekeyerror

提问by jenryb

I have a dataframe that looks just how I want it when I export it to a csv file.

我有一个数据框,当我将它导出到 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

However, when I try to pull from the key 'CompanyName' I get a KeyError: 'CompanyName'I suspect it's being overwritten somewhere but I'm not sure how to fix it.

但是,当我尝试从密钥“公司名称”中提取时,KeyError: 'CompanyName'我怀疑它在某处被覆盖,但我不确定如何修复它。

if I print my dataframe I get:

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

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  

which is rather hard to read to be able to tell what's going on. The code that is throwing the error:

这是相当难以阅读的,以便能够说出正在发生的事情。抛出错误的代码:

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')

EDIT

编辑

Bleh's answer helped eliminate this KeyError. I needed to start the code by resetting the index, because it couldn't call a Key that had been made an index earlier.

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

采纳答案by kennes

This is because you've set the index to CompanyName.

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

You cannot reference the index in that manner.

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

Use pivot_table = pivot_table.reset_index()to reset the index and try accessing it again.

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

Here's the reproduced error:

这是重现的错误:

 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'

Correction Output:

校正输出:

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

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