Python 如何在1414个元素之后的数据帧上解决此关键错误

Python 如何在1414个元素之后的数据帧上解决此关键错误,python,pandas,dataframe,Python,Pandas,Dataframe,你好,我的名为CAR的数据帧有2213行,但是每当我尝试访问第1413个元素后面的行时,我就会得到一个错误。到目前为止,我还没有在网站上发现类似的错误,但我的数据帧是相当标准的(没有Na,只有字符串或整数,大小为7×2213),所以我不知道如何解决这个问题 CAR['Code RS'][1414] len(CAR) --------------------------------------------------------------------------- KeyError

你好,我的名为CAR的数据帧有2213行,但是每当我尝试访问第1413个元素后面的行时,我就会得到一个错误。到目前为止,我还没有在网站上发现类似的错误,但我的数据帧是相当标准的(没有Na,只有字符串或整数,大小为7×2213),所以我不知道如何解决这个问题

CAR['Code RS'][1414]
len(CAR)

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-22-0cf1ffa9a735> in <module>
----> 1 CAR['Code RS'][1414]
      2 len(CAR)

~\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    869         key = com.apply_if_callable(key, self)
    870         try:
--> 871             result = self.index.get_value(self, key)
    872 
    873             if not is_scalar(result):

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   4403         k = self._convert_scalar_indexer(k, kind="getitem")
   4404         try:
-> 4405             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4406         except KeyError as e1:
   4407             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 1414
CAR['code RS'][1414]
len(汽车)
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
在里面
---->1辆车['RS'代码][1414]
2列(汽车)
~\Anaconda3\lib\site packages\pandas\core\series.py in\uuuuu getitem\uuuuuuuuuu(self,key)
869 key=com.apply\u如果可调用(key,self)
870尝试:
-->871结果=self.index.get_值(self,key)
872
873如果不是标量(结果):
获取值中的~\Anaconda3\lib\site packages\pandas\core\index\base.py(self、series、key)
4403 k=self.\u convert\u scalar\u indexer(k,kind=“getitem”)
4404尝试:
->4405返回self.\u engine.get\u值(s,k,tz=getattr(series.dtype,“tz”,None))
4406除键错误为e1外:
4407如果len(self)>0且(self.holds_integer()或self.is_boolean()):
pandas\\u libs\index.pyx在pandas.\u libs.index.IndexEngine.get\u value()中
pandas\\u libs\index.pyx在pandas.\u libs.index.IndexEngine.get\u value()中
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.Int64HashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.Int64HashTable.get\u item()中
关键字错误:1414
如果有帮助的话,我正在使用Jupyter笔记本。
谢谢。

您需要重置数据帧的索引:

CAR=CAR.reset\u索引(drop=True)

为了说明这个问题,以下是一个示例:

df=pd.DataFrame({'a':[1,2],'b':[6,7]})
df2=df.copy()
让我们将它们连接起来。仔细查看生成的
数据帧的索引

df3=pd.concat([df,df2])
我们可以通过重置索引来解决这一问题:

df4=df3.重置索引(drop=True)
这只是一个测试用例场景,可能还有其他原因导致此问题,例如:

df4.drop([2])

尝试重置索引:
CAR=CAR.reset\u index(drop=True)
。谢谢,它成功了!你知道问题出在哪里吗?我提交了答案。如果您能将其标记为已接受,我将不胜感激。
   a  b
0  1  6
1  2  7
   a  b
0  1  6
1  2  7
   a  b
0  1  6
1  2  7
0  1  6
1  2  7
   a  b
0  1  6
1  2  7
2  1  6
3  2  7
   a  b
0  1  6
1  2  7
3  2  7