[KeyError对于数据框中的对象值

问题描述 投票:1回答:1

有人可以解释为什么“路易斯维尔”返回KeyError吗?据我了解,它在数据框中。我想念什么?

Here is what the data looks like. It is a CSV.

This is what off_data.head() looks like

off_data.index()

off_data.columns

编辑:抱歉,这可能是一团糟,不确定我在Stackoverflow中正在做什么

输入

import pandas as pd

off_data = pd.read_csv(r'C:\Users\westc\Desktop\sports.data\ncaab\kenpomdata\off20.csv', index_col= 'Team')

type(off_data)

off_data.loc["Louisville",0]

输出

KeyError Traceback(最近的呼叫最后)〜\ Anaconda3 \ lib \ site-packages \ pandas \ core \ indexes \ base.py在get_loc(self,key,method,tolerance)2896试试:-> 2897返回self._engine.get_loc(key)2898,但KeyError:

pandas._libs.index.IndexEngine.get_loc()中的pandas_libs \ index.pyx>

pandas._libs.index.IndexEngine.get_loc()中的pandas_libs \ index.pyx>

pandas_libs \ hashtable_class_helper.pxi inpandas._libs.hashtable.PyObjectHashTable.get_item()

pandas_libs \ hashtable_class_helper.pxi inpandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError:'路易斯维尔'

在处理以上异常期间,发生了另一个异常:

KeyError Traceback(最近的呼叫最后)45种(off_data)----> 6 off_data.loc [“路易斯维尔”,0]

〜\ Anaconda3 \ lib \ site-packages \ pandas \ core \ indexing.py在getitem

((自身,键))1416除了(KeyError,IndexError,AttributeError):1417通过-> 1418返回self._getitem_tuple(key)1419否则:1420#根据定义,我们只有第0个轴

〜\ Anaconda3 \ lib \ site-packages \ pandas \ core \ indexing.py在_getitem_tuple(自我,tup)803 def _getitem_tuple(self,tup):804尝试:-> 805返回self._getitem_lowerdim(tup)806除了IndexingError:807通过

〜\ Anaconda3 \ lib \ site-packages \ pandas \ core \ indexing.py在_getitem_lowerdim(自我,tup)927 for i,输入enumerate(tup):第928章真相大白(四更)-> 929节= self._getitem_axis(key,axis = i)930931#我们产生了一个标量?

〜\ Anaconda3 \ lib \ site-packages \ pandas \ core \ indexing.py在_getitem_axis(self,key,axis)1848#直查找1849 self._validate_key(key,axis)-> 1850返回self._get_label(键,轴=轴)1851 1852

〜\ Anaconda3 \ lib \ site-packages \ pandas \ core \ indexing.py在_get_label(自我,标签,轴)158提高IndexingError(“这里没有切片,在其他地方处理”)159-> 160返回self.obj._xs(label,axis = axis)161162 def _get_loc(self,key:int,axis:int):

〜\ Anaconda3 \ lib \ site-packages \ pandas \ core \ generic.py in xs(self,key,轴,水平,drop_level)3735 loc,new_index =self.index.get_loc_level(key,drop_level = drop_level)3736其他:-> 3737 loc = self.index.get_loc(key)3738 3739 if isinstance(loc,np.ndarray):

〜\ Anaconda3 \ lib \ site-packages \ pandas \ core \ indexes \ base.py在get_loc(self,key,method,tolerance)2897返回self._engine.get_loc(key)2898`

有人可以解释为什么“路易斯维尔”返回KeyError吗?据我了解,它在数据框中。我想念什么?数据如下所示。这是CSV。这就是off_data ....

python pandas dataframe keyerror
1个回答
0
投票

您可以通过以下方式获得该行:

off_data.loc[off_data['Team'] == "Louisville"]
© www.soinside.com 2019 - 2024. All rights reserved.