gpt4 book ai didi

python - 数据框中的KeyError

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:51 25 4
gpt4 key购买 nike

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

关于python - 数据框中的KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31167896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com